【发布时间】:2017-10-14 00:16:27
【问题描述】:
我需要在 FireFox 中使用 Selenium 在网页中选择这些字段。 HTML页面就像 Sample HTML Page, another sample page.
我使用了以下代码:
driver.findElement(By.id("list_item-2221889")).click();
我正在传递 a-tag 的 id,但这不起作用。有谁知道选择这些 a-tags 的更好方法?
我得到的异常如下:
Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate element: #list_item\-2221889
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: 'unknown', revision: 'unknown', time: 'unknown'
System info: host: 'somePc', ip: '10.8.0.206', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_101'
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities [{moz:profile=C:\Users\User\AppData\Local\Temp\rust_mozprofile.dYp9vdWr9LgT, rotatable=false, timeouts={implicit=0.0, pageLoad=300000.0, script=30000.0}, pageLoadStrategy=normal, platform=ANY, specificationLevel=0.0, moz:accessibilityChecks=false, acceptInsecureCerts=false, browserVersion=53.0, platformVersion=10.0, moz:processID=14524.0, browserName=firefox, javascriptEnabled=true, platformName=windows_nt}]
Session ID: f9bf7f0c-5f8c-4dce-a41c-0cf86c8f5420
*** Element info: {Using=id, value=list_item-2221889}
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at org.openqa.selenium.remote.http.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:150)
at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:115)
at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:45)
at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:164)
at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:82)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:637)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:410)
at org.openqa.selenium.remote.RemoteWebDriver.findElementById(RemoteWebDriver.java:453)
at org.openqa.selenium.By$ById.findElement(By.java:218)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:402)
【问题讨论】:
-
抛出什么样的错误或异常?您可以将此添加到您的问题中吗?
-
您能否刷新页面并检查
id值是否更改?如果是这样,请尝试driver.findElement(By.xpath("//a[starts-with(@id, 'list_item-')]")).click(); -
@Andersson :不,刷新页面时 id 不会改变。
-
试试
driver.findElement(By.cssSelector("div.row div.col-md-4 ul.list-group a")).click();
标签: java html selenium selenium-webdriver