Java 使用selenium选择网页中的元素

Java 使用selenium选择网页中的元素,java,html,selenium,selenium-webdriver,Java,Html,Selenium,Selenium Webdriver,我需要在FireFox中使用Selenium在网页中选择这些字段。HTML页面类似于 , 我使用了以下代码: driver.findElement(By.id("list_item-2221889")).click(); 我正在传递a-tag的id,但这不起作用。有人知道选择这些a标签的更好方法吗 我得到的例外情况如下: Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locat

我需要在FireFox中使用Selenium在网页中选择这些字段。HTML页面类似于 ,

我使用了以下代码:

driver.findElement(By.id("list_item-2221889")).click();
我正在传递a-tag的id,但这不起作用。有人知道选择这些a标签的更好方法吗

我得到的例外情况如下:

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)

您可以等待一段时间,直到元素出现在
DOM
中并可单击:

WebDriverWait wait = new WebDriverWait(driver, 15);
wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("#list_item-2221889"))).click();

如果出现
TimeOut
异常,请检查目标链接是否位于
iframe
内。如果是这样,在处理链接之前,您需要切换到
iframe

driver.switchTo().frame("iframeNameOrId");
driver.findElement(By.id("list_item-2221889")).click();
您可以使用此方法单击所有a标记。如果只想单击第一个a标记,请使用

driver.findElement(By.xPath("//a[@class='list-group-item'][1]")).click();

(语言是C#,抱歉)

一个简单的想法是使用xpath并定位元素

例如,如果需要单击列表中的第一个元素

driver.findElement(By.xPath("//ul[@class='list-group']/a[1]).click();
对于第二个元素,等等

 driver.findElement(By.xPath("//ul[@class='list-group']/a[2]).click();
如果加载表需要时间,请使用显式等待。试试这个,让我知道。
谢谢。

引发了什么样的错误或异常?你们能把这个添加到你们的问题中吗?我需要像这样选择元素的某些部分:你们能刷新页面并检查
id
值是否改变了吗?如果是这样,请尝试
driver.findElement(By.xpath(“//a[以(@id,'list_item-'))开头)。单击()
@Andersson:No刷新页面时id不会更改。请尝试使用
driver.findElement(通过.cssSelector(“div.row div.col-md-4 ul.list-group a”)。单击()
如果您获得了
超时
,那么您的元素很可能位于
内。检查链接的祖先
 driver.findElement(By.xPath("//ul[@class='list-group']/a[2]).click();