报错:
Exception in thread "main" org.openqa.selenium.InvalidSelectorException: The given selector .//*[@id='2']/h3/a/text() is either invalid or does not result in a WebElement. The following error occurred:
InvalidSelectorError: The result of the xpath expression ".//*[@id='2']/h3/a/text()" is: [object Text]. It should be an element.
Command duration or timeout: 31 milliseconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/invalid_selector_exception.html
Build info: version: '2.48.2', revision: '41bccdd', time: '2015-10-09 19:55:52'
System info: host: 'SHNDTPDL0883', ip: '10.225.117.12', os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1', java.version: '1.7.0_55'
*** Element info: {Using=xpath, value=.//*[@id='2']/h3/a/text()}
Session ID: f92e4f2b-43be-4fba-abfb-6ec3d6bea5f5
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities [{platform=WINDOWS, acceptSslCerts=true, javascriptEnabled=true, cssSelectorsEnabled=true, databaseEnabled=true, browserName=firefox, handlesAlerts=true, nativeEvents=false, webStorageEnabled=true, rotatable=false, locationContextEnabled=true, applicationCacheEnabled=true, takesScreenshot=true, version=43.0.3}]
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:206)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:158)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:647)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:353)
at org.openqa.selenium.remote.RemoteWebDriver.findElementByXPath(RemoteWebDriver.java:490)
at org.openqa.selenium.By$ByXPath.findElement(By.java:361)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:345)
at TestPackage.Test2.main(Test2.java:33)
Caused by: org.openqa.selenium.InvalidSelectorException: The given selector .//*[@id='2']/h3/a/text() is either invalid or does not result in a WebElement. The following error occurred:
InvalidSelectorError: The result of the xpath expression ".//*[@id='2']/h3/a/text()" is: [object Text]. It should be an element.
For documentation on this error, please visit: http://seleniumhq.org/exceptions/invalid_selector_exception.html
Build info: version: '2.48.2', revision: '41bccdd', time: '2015-10-09 19:55:52'
System info: host: 'SHNDTPDL0883', ip: '10.225.117.12', os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1', java.version: '1.7.0_55'
Driver info: driver.version: unknown
at <anonymous class>.FirefoxDriver.annotateInvalidSelectorError_(file:///C:/Users/275873/AppData/Local/Temp/anonymous3656865153537910253webdriver-profile/extensions/fxdriver@googlecode.com/components/driver-component.js:10633)
at <anonymous class>.FirefoxDriver.prototype.findElementInternal_(file:///C:/Users/275873/AppData/Local/Temp/anonymous3656865153537910253webdriver-profile/extensions/fxdriver@googlecode.com/components/driver-component.js:10664)
at <anonymous class>.FirefoxDriver.prototype.findElement(file:///C:/Users/275873/AppData/Local/Temp/anonymous3656865153537910253webdriver-profile/extensions/fxdriver@googlecode.com/components/driver-component.js:10668)
at <anonymous class>.DelayedCommand.prototype.executeInternal_/h(file:///C:/Users/275873/AppData/Local/Temp/anonymous3656865153537910253webdriver-profile/extensions/fxdriver@googlecode.com/components/command-processor.js:12534)
at <anonymous class>.DelayedCommand.prototype.executeInternal_(file:///C:/Users/275873/AppData/Local/Temp/anonymous3656865153537910253webdriver-profile/extensions/fxdriver@googlecode.com/components/command-processor.js:12539)
at <anonymous class>.DelayedCommand.prototype.execute/<(file:///C:/Users/275873/AppData/Local/Temp/anonymous3656865153537910253webdriver-profile/extensions/fxdriver@googlecode.com/components/command-processor.js:12481)
感觉上好像是要点的URL没找到。。。求大神帮忙-。-
作者: houyunfeng 时间: 2016-1-4 17:33
".//*[@id='2']/h3/a/text()" is: [object Text]. It should be an element. 这句话说得很明显了,你通过这种方式 driver.findElement(By.xpath(".//*[@id='2']/h3/a/text()")); 最终的结果是找到一个 元素,而不是元素的text值;你想要得到元素的值,是先要找到元素,然后用这个元素再去活动值;
Element element= driver.findElement(By.xpath(".//*[@id='2']/h3/a/")); --这里先获取到元素 对象
element.getText()--再用 获取到的元素对象调用getTest()方法去获取text值作者: Azlken 时间: 2016-1-5 14:46
还是有问题,HTML代码如下
<h3 class="t c-gap-bottom-small">
<a target="_blank" href="http://www.baidu.com/link?url=R7HJnKkfFHYA6Roqw2KpdaRoC_eVNeRh1BBUte-KvpURCZ0wBPt-DJ09p28aoCSEt9Em85oC7hj5zvl8YIyUU_B2nI7i4uKLiLrFgJBWpRm">
<em>hello world</em>
_百度百科
</a>
</h3>
当执行 WebDriver driver=new FirefoxDriver();
driver.get("http://www.baidu.com");
driver.manage().window().maximize();
driver.findElement(By.id("kw")).clear();
driver.findElement(By.id("kw")).sendKeys("Hello world");
//Open Baidu and search Hello world
WebElement element = driver.findElement(By.cssSelector(".c-gap-bottom-small>a>em"));
System.out.println(element.getText());
时,成功输出hello world
但是当执行 WebDriver driver=new FirefoxDriver();
driver.get("http://www.baidu.com");
driver.manage().window().maximize();
driver.findElement(By.id("kw")).clear();
driver.findElement(By.id("kw")).sendKeys("Hello world");
//Open Baidu and search Hello world
//WebElement element = driver.findElement(By.cssSelector(".c-gap-bottom-small>a>em"));
//System.out.println(element.getText());
driver.findElement(By.cssSelector(".c-gap-bottom-small>a>em"));
if (driver.findElement(By.cssSelector(".c-gap-bottom-small>a>em")) != null)
{System.out.println("ok");}
时却找不到这个element了,报错如下:
Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"css selector","selector":".c-gap-bottom-small>a>em"}
Command duration or timeout: 429 milliseconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '2.48.2', revision: '41bccdd', time: '2015-10-09 19:55:52'
System info: host: 'SHNDTPDL0883', ip: '10.225.117.12', os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1', java.version: '1.7.0_55'作者: Azlken 时间: 2016-1-5 14:51
页面HTML代码如下:
div id="content_left">
<div id="1" class="result-op c-container xpath-log" data-click="{'p1':'1','rsv_bdr':'0','fm':'albk',rsv_stl:'0'}" data-op="{'y':'E3FFF577'}" mu="http://baike.baidu.com/subview/47227/11216556.htm?fr=aladdin" tpl="bk_polysemy" fk="Hello world" srcid="91">
<h3 class="t c-gap-bottom-small">
<a target="_blank" href="http://www.baidu.com/link?url=R7HJnKkfFHYA6Roqw2KpdaRoC_eVNeRh1BBUte-KvpURCZ0wBPt-DJ09p28aoCSEt9Em85oC7hj5zvl8YIyUU_B2nI7i4uKLiLrFgJBWpRm">
<em>hello world</em>
_百度百科
</a>
</h3>
<div class="c-row">
<div class="c-span6">
<div class="c-span18 c-span-last">
</div>
<div class="c-gap-top c-recommend" style="display:none;">
<i class="c-icon c-icon-bear-circle c-gap-right-small"/>
<span class="c-gray">为您推荐:</span>
<a title="hello world c++" href="/s?rsv_idx=1&wd=hello+world+c%2B%2B&usm=2&ie=utf-8&rsv_cq=Hello+world&rsv_dl=0_left_bk_polysemy_91_19781" target="_blank">hello world c++</a>
<a class="c-gap-left-large" title="hello world 血界战线" href="/s?rsv_idx=1&wd=hello+world+%E8%A1%80%E7%95%8C%E6%88%98%E7%BA%BF&usm=2&ie=utf-8&rsv_cq=Hello+world&rsv_dl=0_left_bk_polysemy_91_19781" target="_blank">hello world 血界战线</a>
<a class="c-gap-left-large" title="hello world 程序" href="/s?rsv_idx=1&wd=hello+world+%E7%A8%8B%E5%BA%8F&usm=2&ie=utf-8&rsv_cq=Hello+world&rsv_dl=0_left_bk_polysemy_91_19781" target="_blank">hello world 程序</a>
</div>
</div>
当执行
WebElement element = driver.findElement(By.cssSelector(".c-gap-bottom-small>a>em"));
System.out.println(element.getText());
时,能够输入hello world
但是当执行
driver.findElement(By.cssSelector(".c-gap-bottom-small>a>em"));
if (driver.findElement(By.cssSelector(".c-gap-bottom-small>a>em")) != null)
{System.out.println("ok");}
时,报错。。。如下:
Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"css selector","selector":".c-gap-bottom-small>a>em"}
Command duration or timeout: 429 milliseconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '2.48.2', revision: '41bccdd', time: '2015-10-09 19:55:52'
System info: host: 'SHNDTPDL0883', ip: '10.225.117.12', os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1', java.version: '1.7.0_55'