我想使用Selenium Webdriver从网页上获取输入标记项。
这是html代码:
<tr>
<td class="listLabel" height="24 px">Username:</td>
<td class="listData">
<input type="text" tabindex="1" name="username" size="30">
</td>
</tr>
<tr>
<td class="listLabel" height="24 px">Password:</td>
<td class="listData">
<input type="password" tabindex="2" name="password" size="30">
<input type="submit" value=" Login " name="submit">
</td>
</tr>
直到现在我所做的是:
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.get(adminURL);
List<WebElement> listElements = driver.findElements(By.xpath("//td[@class='listData']"));
System.out.println(listElements.size()); // gives the element size 2 which is valid.
for(WebElement el:listElements){
el.findElement(By.xpath("//input[text()='username']")); //didnot find the element
}
System.out.println(" Title of the page is : "+driver.getTitle());
我使用xpath获取元素。但它没有使用输入类型和文本用户名获取元素。
它引发了这个异常:
Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"xpath","selector":"//input[text()='username']"}
Command duration or timeout: 113 milliseconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '2.43.1', revision: '5163bce', time: '2014-09-10 16:27:58'
System info: host: 'DS-7071BC8FD7AA', ip: '10.112.94.126', os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1', java.version: '1.7.0_60'
Session ID: c1b5d8b8-43db-4157-8ab6-32286116d89a
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities [{platform=WINDOWS, databaseEnabled=true, cssSelectorsEnabled=true, javascriptEnabled=true, acceptSslCerts=true, handlesAlerts=true, browserName=firefox, webStorageEnabled=true, nativeEvents=true, rotatable=false, locationContextEnabled=true, applicationCacheEnabled=true, takesScreenshot=true, version=32.0.3}]
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:204)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:156)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:599)
at org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:268)
at org.openqa.selenium.remote.RemoteWebElement.findElement(RemoteWebElement.java:171)
at org.openqa.selenium.remote.RemoteWebElement.findElementByXPath(RemoteWebElement.java:244)
at org.openqa.selenium.By$ByXPath.findElement(By.java:357)
at org.openqa.selenium.remote.RemoteWebElement.findElement(RemoteWebElement.java:167)
at com.test.selenese.SeleneseTest.main(SeleneseTest.java:35)
Caused by: org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"xpath","selector":"//input[text()='username']"}
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '2.43.1', revision: '5163bce', time: '2014-09-10 16:27:58'
System info: host: 'DS-7071BC8FD7AA', ip: '10.112.94.126', os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1', java.version: '1.7.0_60'
Driver info: driver.version: unknown
at <anonymous class>.FirefoxDriver.prototype.findElementInternal_(file:///C:/Users/rajan.v/AppData/Local/Temp/anonymous2185375942107103130webdriver-profile/extensions/fxdriver@googlecode.com/components/driver-component.js:9618:133)
at <anonymous class>.FirefoxDriver.prototype.findChildElement(file:///C:/Users/rajan.v/AppData/Local/Temp/anonymous2185375942107103130webdriver-profile/extensions/fxdriver@googlecode.com/components/driver-component.js:9630:3)
at <anonymous class>.DelayedCommand.prototype.executeInternal_/h(file:///C:/Users/rajan.v/AppData/Local/Temp/anonymous2185375942107103130webdriver-profile/extensions/fxdriver@googlecode.com/components/command-processor.js:11612:16)
at <anonymous class>.DelayedCommand.prototype.executeInternal_(file:///C:/Users/rajan.v/AppData/Local/Temp/anonymous2185375942107103130webdriver-profile/extensions/fxdriver@googlecode.com/components/command-processor.js:11617:7)
at <anonymous class>.DelayedCommand.prototype.execute/<(file:///C:/Users/rajan.v/AppData/Local/Temp/anonymous2185375942107103130webdriver-profile/extensions/fxdriver@googlecode.com/components/command-processor.js:11559:5)
支持我正确行事。提前谢谢。
答案 0 :(得分:1)
尝试这种方式:
el.findElement(By.xpath(".//input[@name='username']"));
添加前导.
以告知XPath与当前el
元素相关。
您尝试按属性而非内部文字过滤元素,因此请将text()
替换为@attribute-name