Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/396.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Selenium webdriver:org.openqa.Selenium.InvalidElementStateException:元素已禁用,因此不能用于操作_Java_Selenium_Selenium Webdriver - Fatal编程技术网

Java Selenium webdriver:org.openqa.Selenium.InvalidElementStateException:元素已禁用,因此不能用于操作

Java Selenium webdriver:org.openqa.Selenium.InvalidElementStateException:元素已禁用,因此不能用于操作,java,selenium,selenium-webdriver,Java,Selenium,Selenium Webdriver,我在尝试编写SeleniumWebDriver中的简单代码以在google搜索页面中输入值并输入时遇到此错误。 以下是我的代码-: WebDriver driver = new FirefoxDriver(profile); driver.manage().timeouts().implicitlyWait(40, TimeUnit.SECONDS); driver.get("http://www.google.com"); WebElement element

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

    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();

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

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

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


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

//input[@name='q']


此“input”元素可以很好地接受输入文本。

请参阅,如果这有帮助

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();
}
试试这个:

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);
或:


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

如果我的回答正确,那么您正在使用firefox驱动程序中的firebug插件获取搜索框的路径。但是firebug似乎提供了一条搜索框Id不正确的路径。如果使用inspect element选项,您可以看到差异(在下图中,您可以自己发现差异)


谢谢Andrian,javascript工作正常。但我仍然面临着一个问题。谢谢Andrian,javascript工作正常。但我仍然面临一个问题。函数“sendKeys”以不可编辑的形式输入文本,无法进一步单击搜索按钮或使用“submit()”。如果您想在google中搜索内容,请尝试使用id='gbqfq'或下载Selenium IDE,它将显示google和SensKeys(Key.Enter)的正确输入,以便开始搜索
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();
}
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();
 }