尝试从google搜索editbox获取和打印数据,但是有InvalidElementStateException [英] Trying to get and print data from google search editbox but got InvalidElementStateException

查看:125
本文介绍了尝试从google搜索editbox获取和打印数据,但是有InvalidElementStateException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在google搜索editbox中写一个单词并且捕获所有相关的列表项并显示它。但是在这样做的时候我得到一个例外
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(false);
window = new FirefoxDriver(profile);
尝试{
window.get(https://www.google.co.in/);
window.manage()。window()。maximize();
window.findElement(By.id(gs_htif0))sendKeys(Shantanu Nandan);
window.manage().timeouts()。implicitlyWait(10,TimeUnit.SECONDS);
列表< WebElement> list = window.findElements(By.xpath(// td [@ style ='width:100%;'] / span));
window.manage().timeouts()。implicitlyWait(10,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

我获得此代码的异常

  org.openqa.selenium.InvalidElementStateException:元素被禁用,因此可能不会用于动作
命令持续时间或超时:47毫秒
构建信息:version:'2.39.0',revision:'ff23eac',time:'2013-12-16 16:12:12'
系统信息:主机:'omega-d52b83806',ip:'10.0.0.4',os.name:'Windows XP',os.arch:'x86',os.version:'5.1',java.version :'1.7.0_45'
会话ID:d29ef567-95a8-47ce-bf84-c510cbb2756d
驱动程序信息:org.openqa.selenium.firefox.FirefoxDriver
功能[{platform = XP,acceptSslCerts = true,javascriptEnabled = true,cssSelectorsEnabled = true,databaseEnabled = true,browserName = firefox,handlesAlerts = true,browserConnectionEnabled = true,webStorageEnabled = true,nativeEvents = false,可旋转= false,locationContextEnabled = true,applicationCacheEnabled = true ,takeScreenshot = 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)
java.lang.reflect.Constructor.newInstance(Unknown Source)
在org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:193 )
在org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:145)
在org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:554)
在org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:268)
在org.openqa.selenium.remote.RemoteWebElement.sendKeys(RemoteWebElement.java:89)
在AutoIt。 main(AutoIt.java:21)
导致:org.openqa.selenium.remote.ErrorHandler $ UnknownServerException:元素被禁用,因此可能不会用于操作
构建信息:version:'2.39。 0 ,修改:'ff23eac',时间:'2013-12-16 16:12:12'
系统信息:主机:'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.version:unknown
at< anonymous class> .fxdriver.preconditions.enabled(file:/// C:/DOCUME ~1/OMEGA/LOCALS~1/Temp/anonymous7567378475668954944webdriver-profile/extensions/fxdriver@googlecode.com/components/command_processor.js:8185)
at< anonymous class> .DelayedCommand.prototype.checkPreconditions_(file:/// C:/DOCUME ~1/OMEGA/LOCALS_1/Temp/anonymous7567378475668954944webdriver-profile/extensions/fxdriver@googlecode.com/components/command_processor。 js:10814)
at< anonymous class> .DelayedCommand.prototype.executeInternal_ / h(file:/// C:/ DOCUME〜1 / OMEGA / LOCALS〜1 / Temp / anonymous7567378475668954944webdriver-profile / extensions / fxdriver @ googlecode.com / components / command_processor.js:10831)
在< anonymous class> .De layedCommand.prototype.executeInternal_(file:/// C:/DOCUME ~1/OMEGA/LOCALS~1/Temp/anonymous7567378475668954944webdriver-profile/extensions/fxdriver@googlecode.com/components/command_processor.js:10836)
at< anonymous class> .DelayedCommand.prototype.execute /<(file:/// C:/DOCUME ~1/OMEGA/LOCALS〜1/Temp/anonymous7567378475668954944webdriver-profile/extensions/fxdriver@googlecode.com/components/ command_processor.js:10778)
最后块执行....


解决方案

您选择了错误的元素。



使用下面的搜索文本框

  WB = window.findElement(By.name( q)); 

使用下面的输出建议。

 列表与LT; WebElement> list = window.findElements(By.className(gsq_a));要获取列表中的值,请使用getText()方法代替getAttribute()。




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

 
}


I am trying to write a word in google search editbox and capture all the related list item and display it. But while doing so i got Got a exception org.openqa.selenium.InvalidElementStateException: Element is disabled and so may not be used for actions. I went through the same exception related question and according to them changed my code but none of them help.

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(false);
  window = new FirefoxDriver(profile);
try{
window.get("https://www.google.co.in/");
window.manage().window().maximize();
window.findElement(By.id("gs_htif0")).sendKeys("Shantanu Nandan");
window.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
List<WebElement> list=window.findElements(By.xpath("//td[@style='width: 100%;']/span"));
window.manage().timeouts().implicitlyWait(10,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 for this code

org.openqa.selenium.InvalidElementStateException: Element is disabled and so may not be used for actions
Command duration or timeout: 47 milliseconds
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: d29ef567-95a8-47ce-bf84-c510cbb2756d
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:21)
  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/anonymous7567378475668954944webdriver-profile/extensions/fxdriver@googlecode.com/components/command_processor.js:8185)
at <anonymous class>.DelayedCommand.prototype.checkPreconditions_(file:///C:/DOCUME~1/OMEGA/LOCALS~1/Temp/anonymous7567378475668954944webdriver-profile/extensions/fxdriver@googlecode.com/components/command_processor.js:10814)
at <anonymous class>.DelayedCommand.prototype.executeInternal_/h(file:///C:/DOCUME~1/OMEGA/LOCALS~1/Temp/anonymous7567378475668954944webdriver-profile/extensions/fxdriver@googlecode.com/components/command_processor.js:10831)
at <anonymous class>.DelayedCommand.prototype.executeInternal_(file:///C:/DOCUME~1/OMEGA/LOCALS~1/Temp/anonymous7567378475668954944webdriver-profile/extensions/fxdriver@googlecode.com/components/command_processor.js:10836)
at <anonymous class>.DelayedCommand.prototype.execute/<(file:///C:/DOCUME~1/OMEGA/LOCALS~1/Temp/anonymous7567378475668954944webdriver-profile/extensions/fxdriver@googlecode.com/components/command_processor.js:10778)
Finally Block Executed ....

解决方案

You have selected the wrong element.

Use the below one for search text box

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

Use the below for getting output suggestions.

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

To get the values from the list, use getText() method instead of getAttribute().

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

这篇关于尝试从google搜索editbox获取和打印数据,但是有InvalidElementStateException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆