简体   繁体   English

Selenium webdriver:org.openqa.selenium.InvalidElementStateException:元素已禁用,因此可能无法用于操作

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

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. 尝试在Selenium Webdriver中写入简单代码以在Google搜索页中输入值并输入时,出现了此错误。 Following is my code -: 以下是我的代码-:

    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? 如何启用禁用的元素?

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. 这个'input'元素可以接受输入文本。

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

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

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更改为名称选择器。

If i am correct then you are using the firebug add-on in the firefox driver to get the path for the searchbox. 如果我是正确的,那么您正在使用firefox驱动程序中的firebug插件来获取搜索框的路径。 But the firebug seems to provide a path where the Id for the searchbox is not correct. 但是萤火虫似乎提供了一个路径,其中搜索框的ID不正确。 If you use the inspect element option you can see the difference (in the below image you can spot the difference yourself). 如果使用检查元素选项,则可以看到差异(在下面的图像中,您可以自己发现差异)。

图片。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Selenium 错误:org.openqa.selenium.InvalidElementStateException:无效元素 state - Selenium Error : org.openqa.selenium.InvalidElementStateException: invalid element state org.openqa.selenium.InvalidElementStateException:用于Google搜索 - org.openqa.selenium.InvalidElementStateException: for google search org.openqa.selenium.InvalidElementStateException:无效的元素状态:SyntaxError - org.openqa.selenium.InvalidElementStateException: invalid element state: SyntaxError Selenium:如何解决 org.openqa.selenium.InvalidElementStateException: invalid element state - Selenium : How to solve org.openqa.selenium.InvalidElementStateException: invalid element state 线程“主” org.openqa.selenium.InvalidElementStateException中的异常: - Exception in thread “main” org.openqa.selenium.InvalidElementStateException: org.openqa.selenium.InvalidElementStateException:无法执行本机交互:无法加载本机事件组件 - org.openqa.selenium.InvalidElementStateException:Cannot perform native interaction: Could not load native events component 线程“主” org.openqa.selenium.ElementNotVisibleException中的异常:元素当前不可见,因此可能无法与之交互 - Exception in thread “main” org.openqa.selenium.ElementNotVisibleException: Element is not currently visible and so may not be interacted with org.openqa.selenium.ElementNotVisibleException:元素当前不可见,因此可能无法与之交互 - org.openqa.selenium.ElementNotVisibleException: Element is not currently visible and so may not be interacted with org.openqa.selenium.ElementNotVisibleException:元素当前不可见,因此可能无法与Command持续时间或超时进行交互: - org.openqa.selenium.ElementNotVisibleException: Element is not currently visible and so may not be interacted with Command duration or timeout: webdriver org.openqa.selenium.NoSuchElementException:无法找到元素: - webdriver org.openqa.selenium.NoSuchElementException: Unable to locate element:
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM