[英]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.