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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20772696/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 04:13:48  来源:igfitidea点击:

Selenium webdriver :org.openqa.selenium.InvalidElementStateException: Element is disabled and so may not be used for actions

javaseleniumselenium-webdriver

提问by user3134443

I am getting this error while trying to write to simple code in selenium webdriver to enter a value in google search page and enter. Following is my code -:

我在尝试写入 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();

Can anyone please help me out with this? How to enable a disabled element?

任何人都可以帮我解决这个问题吗?如何启用禁用的元素?

回答by Andrian Durlestean

You can try to run javascript on page:

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

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

or

或者

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

回答by Vlad.Bachurin

You are using the wrong 'input' for entering the text. You should be using the following XPath:

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

//input[@name='q']

Like

喜欢

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

This 'input' element accepts the input text just fine.

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

回答by user3190584

See, if this might help,

看,如果这可能有帮助,

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

回答by Milky

Try this:

尝试这个:

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

Or:

或者:

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

Or change xPath to name selector.

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

回答by sanyam sharma

If i am correct then you are using the firebug add-on in the firefox driver to get the path for the searchbox. But the firebug seems to provide a path where the Id for the searchbox is not correct. If you use the inspect element option you can see the difference (in the below image you can spot the difference yourself).

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

image.

图片。