java - Google editboix 给出 InvalidElementStateException :Element is disabled and so may not be used for actions Command duration

标签 java exception selenium selenium-webdriver selenium-ide

我正在尝试在谷歌搜索编辑框中写入一个单词并捕获所有相关的列表项并显示它。但在这样做时,我得到了一个异常 org.openqa.selenium.InvalidElementStateException:元素已禁用,因此可能无法用于操作。我经历了相同的异常相关问题,并根据他们更改了我的代码,但没有一个有帮助。

import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver; 
import org.openqa.selenium.firefox.FirefoxProfile;

 public class AutoIt {
   public static void main(String[] args){
  WebDriver window;
  FirefoxProfile profile = new FirefoxProfile();
  profile.setEnableNativeEvents(true);
  window = new FirefoxDriver(profile);
  window.manage().timeouts().implicitlyWait(40,TimeUnit.SECONDS);
  WebElement wb;
try{
window.get("https://www.google.co.in/");
window.manage().window().maximize();
wb=window.findElement(By.xpath(".//input[@id='gs_htif0']"));
if(wb.isEnabled()) {
    System.out.println("Enable");
}//if
else{
    System.out.println("Disable");
}//else
wb.click();
wb.sendKeys("Shantanu Nandan");
window.manage().timeouts().implicitlyWait(40,TimeUnit.SECONDS);
List<WebElement> list=window.findElements(By.xpath("//td[@style='width: 400%;']/span"));
window.manage().timeouts().implicitlyWait(40,TimeUnit.SECONDS);
for(WebElement data:list){
    System.out.println(data.getAttribute("value"));

 }//for
}//try
catch(Exception e){
System.out.println("Got a exception");
    e.printStackTrace();
    window.close();
}//catch
finally{
    System.out.println("Finally Block Executed ....");
    window.close();
    window.quit();
}//finally
   }//main
 }//class
Exception Which i got-
   org.openqa.selenium.InvalidElementStateException: Element is disabled and so may not be used for actions
   Command duration or timeout: 40.06 seconds
   Build info: version: '2.39.0', revision: 'ff23eac', time: '2013-12-16 16:12:12'
   System info: host: 'omega-d52b83806', ip: '10.0.0.4', os.name: 'Windows XP', os.arch: 'x86',   os.version: '5.1', java.version: '1.7.0_45'
    Session ID: aa82373a-1d12-4000-b89a-2238c83d4e06
  Driver info: org.openqa.selenium.firefox.FirefoxDriver
  Capabilities [{platform=XP, acceptSslCerts=true, javascriptEnabled=true, cssSelectorsEnabled=true, databaseEnabled=true, browserName=firefox, handlesAlerts=true, browserConnectionEnabled=true, webStorageEnabled=true, nativeEvents=false, rotatable=false, locationContextEnabled=true, applicationCacheEnabled=true, takesScreenshot=true, version=29.0.1}]
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:193)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:145)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:554)
at org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:268)
at org.openqa.selenium.remote.RemoteWebElement.sendKeys(RemoteWebElement.java:89)
at AutoIt.main(AutoIt.java:28)
   Caused by: org.openqa.selenium.remote.ErrorHandler$UnknownServerException: Element is disabled and so may not be used for actions
   Build info: version: '2.39.0', revision: 'ff23eac', time: '2013-12-16 16:12:12'
   System info: host: 'omega-d52b83806', ip: '10.0.0.4', os.name: 'Windows XP', os.arch: 'x86',   os.version: '5.1', java.version: '1.7.0_45'
    Driver info: driver.version: unknown
at <anonymous class>.fxdriver.preconditions.enabled(file:///C:/DOCUME~1/OMEGA/LOCALS~1/Temp/anonymous4150390975453553730webdriver-profile/extensions/<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5d3b25392f342b382f1d3a32323a31383e323938733e3230" rel="noreferrer noopener nofollow">[email protected]</a>/components/command_processor.js:8185)
at <anonymous class>.DelayedCommand.prototype.checkPreconditions_(file:///C:/DOCUME~1/OMEGA/LOCALS~1/Temp/anonymous4150390975453553730webdriver-profile/extensions/<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b4d2ccd0c6ddc2d1c6f4d3dbdbd3d8d1d7dbd0d19ad7dbd9" rel="noreferrer noopener nofollow">[email protected]</a>/components/command_processor.js:10814)
at <anonymous class>.DelayedCommand.prototype.executeInternal_/h(file:///C:/DOCUME~1/OMEGA/LOCALS~1/Temp/anonymous4150390975453553730webdriver-profile/extensions/<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="9afce2fee8f3ecffe8dafdf5f5fdf6fff9f5feffb4f9f5f7" rel="noreferrer noopener nofollow">[email protected]</a>/components/command_processor.js:10831)
at <anonymous class>.fxdriver.Timer.prototype.setTimeout/<.notify(file:///C:/DOCUME~1/OMEGA/LOCALS~1/Temp/anonymous4150390975453553730webdriver-profile/extensions/<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5f39273b2d36293a2d1f38303038333a3c303b3a713c3032" rel="noreferrer noopener nofollow">[email protected]</a>/components/command_processor.js:396)

最佳答案

您选择了错误的元素。

使用下面的搜索文本框

wb=window.findElement(By.name("q"));

使用以下内容获取输出建议。

List<WebElement> list = window.findElements(By.className("gsq_a"));

要从列表中获取值,请使用 getText() 方法而不是 getAttribute()。

for (WebElement data : list) {
   System.out.println(data.getText());
}

关于java - Google editboix 给出 InvalidElementStateException :Element is disabled and so may not be used for actions Command duration,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23959890/

相关文章:

用于移动设备登录 session 的 Java EE 标准?

java - 摆脱无意的“actionPerformed : code in Java Netbeans

c# - Selenium 等待元素等于某物

java - 使用静态的 Selenium 框架

python - 无法在服务器上使用 Chrome 驱动程序运行 selenium 应用程序

java - 滑动拼图,开关图标

python - 异常子类的打印不是参数元组

java - JSF 1.2 Action 异常和 actionListener 的区别

.net - 跨服务边界传递异常,是个坏主意吗?

java - 您可以在自动配置的 Spring Boot H2 测试数据库上设置兼容模式吗?