使用selenium在网页中选择元素

时间:2017-05-15 09:19:24

标签: java html selenium selenium-webdriver

我需要在FireFox中使用Selenium在网页中选择这些字段。 HTML页面就像 Sample HTML Pageanother sample page.

我使用了以下代码:

driver.findElement(By.id("list_item-2221889")).click();

我正在传递a-tag的id,但这不起作用。有谁知道选择那些标签有更好的方法吗?

我得到的例外情况如下:

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)

3 个答案:

答案 0 :(得分:1)

您可以等待一段时间,直到元素出现在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();

答案 1 :(得分:0)

fmod

您可以使用此方法单击所有a-tags。如果您只想首先点击标签,请使用;

var aTags = driver.findElements(By.xPath("//a[@class='list-group-item']"));

foreach(var aTag in aTags)
{
aTag.click();
}

(语言是C#,对不起)

答案 2 :(得分:0)

一个简单的想法是使用xpath并找到元素。

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

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

和第二个元素等等

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

如果表需要时间加载,请使用显式等待。试试这个,让我知道。 感谢。