【问题标题】:Select element in webpage using selenium使用硒选择网页中的元素
【发布时间】: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


【解决方案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();

【讨论】:

  • 如果你得到TimeOut,那么你的元素很可能位于<iframe>中。检查链接的祖先
【解决方案2】:
var aTags = driver.findElements(By.xPath("//a[@class='list-group-item']"));

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

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

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

(语言是 C#,抱歉)

【讨论】:

    【解决方案3】:

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

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

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

    对于第二个元素,依此类推

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-02-21
      • 2012-01-09
      • 2018-12-05
      • 1970-01-01
      • 1970-01-01
      • 2019-04-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多