【问题标题】:Selenium 2 driver.findElement()Selenium 2 驱动程序.findElement()
【发布时间】:2013-05-03 17:20:39
【问题描述】:

这怎么可能

driver.findElement(By.cssSelector("input[id*='supplierOps_input'][class*='ui-autocomplete-input']")); 

抛出这个异常:org.openqa.selenium.NoSuchElementException : Returned node was not an HTML element.

还有

driver.findElements(By.cssSelector("input[id*='supplierOps_input'][class*='ui-autocomplete-input']"));

效果很好,他的 .size() 返回 1(所以我只有 1 个元素),我可以使用 .get(0).getAttribute("id"); 获取 id 值

我的大脑在挣扎。


更新:

这是 POM.xml 中的 Selenium 部分

<dependency>
  <groupId>org.seleniumhq.selenium</groupId>
  <artifactId>selenium-java</artifactId>
  <scope>test</scope>
</dependency>
<dependency>
  <groupId>org.seleniumhq.selenium</groupId>
  <artifactId>selenium-server</artifactId>
  <scope>test</scope>
  <exclusions>
    <exclusion>
      <groupId>org.mortbay.jetty</groupId>
      <artifactId>servlet-api-2.5</artifactId>
    </exclusion>
  </exclusions>
</dependency>

【问题讨论】:

  • 您使用哪个版本的 Selenium?
  • 我的猜测是:因为您还在 Css 选择器中搜索 class - 默认情况下,可能有多个具有此类的元素,这就是 findElement 失败的原因。但这真的是猜测
  • 您是否尝试过将其转换为等效的 XPath 查询?我想知道它是否返回相同的东西。
  • @skirsch 实际上,我发现我的pom.xml 中没有任何&lt;version&gt;selenium's version&lt;/version&gt;
  • @Arran 我已经回答了我自己的问题。

标签: java webdriver css-selectors selenium-webdriver


【解决方案1】:

好吧,我的脑子又疼了,因为在这个问题后 2 天,我尝试做同样的事情,findElement(By.cssSelector())findElements(By.CssSelector()) 现在都运行良好。

我也尝试过使用 xpath 并且效果也很好,例如:

driver.findElement(By.xpath("//input[contains(@id,'supplierOps_input') and contains(@class,'ui-autocomplete-input')]"))

这可能不是一个真正的答案,因为我不知道为什么我第一次尝试使用 cssSelector 时给了我一个投掷:org.openqa.selenium.NoSuchElementException : Returned node was not an HTML element.

也许它不稳定,有时它会很酷,有时它会抛出..

【讨论】:

  • 您是否在同一个未更改的网站上运行过它?
  • @skirsch 是的,除了findElement(),我什么都没改变
猜你喜欢
  • 1970-01-01
  • 2011-11-19
  • 2011-09-16
  • 1970-01-01
  • 1970-01-01
  • 2022-01-28
  • 1970-01-01
  • 1970-01-01
  • 2017-09-11
相关资源
最近更新 更多