【问题标题】:Selenium webdriver :org.openqa.selenium.InvalidElementStateException: Element is disabled and so may not be used for actionsSelenium webdriver :org.openqa.selenium.InvalidElementStateException: 元素被禁用,因此不能用于操作
【发布时间】:2013-12-25 12:15:59
【问题描述】:

我在尝试写入 selenium webdriver 中的简单代码以在谷歌搜索页面中输入值并输入时遇到此错误。 以下是我的代码 - :

    WebDriver driver = new FirefoxDriver(profile);
    driver.manage().timeouts().implicitlyWait(40, TimeUnit.SECONDS);
    driver.get("http://www.google.com");

    WebElement element=driver.findElement(By.xpath("//input[@id='gs_htif0']"));
        boolean b = element.isEnabled();

    if (b){

        System.out.println("Enabled");
    }

    element.sendKeys("Test Automation");

    element.submit();

谁能帮我解决这个问题?如何启用禁用的元素?

【问题讨论】:

    标签: java selenium selenium-webdriver


    【解决方案1】:

    您在输入文本时使用了错误的“输入”。您应该使用以下 XPath:

    //input[@name='q']
    

    喜欢

    WebElement element=driver.findElement(By.xpath("//input[@name='q']"));
    

    这个 'input' 元素可以很好地接受输入文本。

    【讨论】:

      【解决方案2】:

      您可以尝试在页面上运行javascript:

      ((JavascriptExecutor) driver).executeScript("document.getElementById('gs_htif0').disabled = false");
      

      ((JavascriptExecutor) driver).executeScript("arguments[0].disabled = false", element);
      

      【讨论】:

      • 感谢 Andrian,javascript 工作正常。但我仍然面临一个问题。
      • 感谢 Andrian,javascript 工作正常。但我仍然面临一个问题。函数“sendKeys”正在以不可编辑的形式输入文本,无法进一步点击“搜索”按钮或使用“submit()”。
      • 如果您想在 google 中搜索某些内容,请尝试使用 id='gbqfq' 或下载 Selenium IDE,它将显示正确的 google 输入和 SensKeys(Key.Enter) 以开始搜索
      【解决方案3】:

      看看,如果这可能有帮助,

      WebDriver driver = new FirefoxDriver(profile);
      
      driver.manage().timeouts().implicitlyWait(40, TimeUnit.SECONDS);
      
      driver.get("http://www.google.com");
      
      WebElement element = driver.findElement(By.name("q"));
      
      if(element.isEnabled()) {
          System.out.println("Enabled");
          element.sendKeys("Test Automation");
          element.submit();
      }
      

      【讨论】:

        【解决方案4】:

        试试这个:

        WebDriverWait wait = new WebDriverWait(driver, 40);
        driver.get("http://www.google.com");
        
        wait.until(ExpectedConditions.visibilityOfElementLocated(By
                        .xpath("//input[@id='gs_htif0']")));
        driver.findElement(By.xpath("//input[@id='gs_htif0']"))
                        .sendKeys("Test Automation" + Keys.ENTER);
        

        或者:

        public boolean isElementPresent(WebDriver driver, By by)
        {
        try {
                    driver.findElement(by);
                    System.out.print("Enabled");
                    return true;
                  } catch (NoSuchElementException ignored) {  
                    return false;
                  }
        }
        
        isElementPresent = isElementPresent(
                        driver, By.xpath("//input[@id='gs_htif0']"));
        if (isElementPresent) { 
            element.sendKeys("Test Automation");
            element.submit();
         }
        

        或将 xPath 更改为名称选择器。

        【讨论】:

          【解决方案5】:

          如果我是正确的,那么您正在使用 firefox 驱动程序中的 firebug 插件来获取搜索框的路径。但是萤火虫似乎提供了一条路径,其中搜索框的 ID 不正确。如果您使用检查元素选项,您可以看到差异(在下图中,您可以自己发现差异)。

          【讨论】:

            猜你喜欢
            • 2020-11-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2015-06-08
            • 2012-05-25
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多