【问题标题】:differences between InvalidSelectorException and NoSuchElementException and locating itemsInvalidSelectorException 和 NoSuchElementException 和定位项之间的区别
【发布时间】:2021-01-08 18:26:36
【问题描述】:

我每晚通过 Jenkins 运行测试。大约 95% 的时间它都会过去。但有时它会失败:

driver.findElement(By.xpath("//div[contains(@id, 'stage')]/div[contains(@class, 'tabMenu')]/span[contains(@class, 'menuItem') and contains(text(),'Employer')]"))

带有 org.openqa.selenium.InvalidSelectorException。注意:这几乎总是有效的。

所以我使用 Eclipse、Java 和 Selenium 进行调试。同样,在出现此错误之前,我必须运行大约 30 次测试。

我正在使用 Selenium 和 IE。 IE 没有很好的调试功能,因此使用 Eclipse Debug Shell 我将页面源代码写入文件,将文件重命名为 html 文件并使用 Chrome 进行编辑。在检查窗口中,我搜索

 //div[contains(@id, 'stage')]/div[contains(@class, 'tabMenu')]/span[contains(@class, 'menuItem') and contains(text(),'Employer')]

它会找到它并突出显示它。当我从调试外壳做一个

 driver.findElement(By.xpath("//div[contains(@id, 'stage')]/div[contains(@class, 'tabMenu')]/span[contains(@class, 'menuItem') and contains(text(),'Employer')]"))

我明白了

 org.openqa.selenium.InvalidSelectorException:Unable to locate an element ... because of the following error:
 [object Error] (WARNING: The server did not provide any stacktrace information)

所以我想我会得到所有的 div 并得到他们的 ID:

 driver.findElement(By.xpath("//div"))

我明白了

 org.openqa.selenium.InvalidSelectorException: Unable to locate an element with the xpath expression //div because of the following error:
[object Error] (WARNING: The server did not provide any stacktrace information)

没问题。我尝试 driver.findElement(By.xpath("//*")) 仍然得到 InvalidSelectorException

所以“//*”必须找到元素。即使没有,它不应该返回类似 NoSuchElementException 而不是 InvalidSelectorException 的东西吗?有谁知道发生了什么?我进行了搜索以查找 InvalidSelectorException 的含义,并找到了用法但不是真正的定义。

所以我想我有两个问题:1. 为什么没有找到任何元素(甚至“//*”),以及 2. 它不应该返回 NoSuchElementException 而不是 InvalidSelectorExmaple?

【问题讨论】:

  • 你给驱动程序一个相当混乱的 xpath...它混合了一个基于路径的选择器,指示某个 div 之后的第一个跨度(具有相对要求)和一个必须包含两个值的指令。找到一个不那么脆弱的 xpath 并可能在那里添加一个 webdriverwait ,因为这种情况有时会发生,这表明存在时间问题。发布您要定位的元素的 HTML。 (“//*”并没有真正指向任何特定元素,这可能是它返回无效的原因。这就像说“返回 1 个任何地方的任何元素”也许 findElements 会起作用......)

标签: selenium invalidselectorexception


【解决方案1】:

InvalidSelectorExceptionNoSuchElementException 是完全不同的例外。

如您所见,当xpath 无效时会引发此异常。

为什么你会收到InvalidSelectorException

您提到在 IE 上运行测试,但是 IE 浏览器没有用于查找元素的本机 xpath 引擎。因此,为了能够在 IE 中通过 xpath 查找元素,驱动程序必须使用 JavaScript xpath 查询引擎。

我知道以上对于 IE 版本直到包括 9 都是正确的。不确定 IE10 和 IE11,但您也没有提及您使用的 IE 版本,所以这可能是根本原因之一。

您的 xpath 在语法上是正确的,但 IE 可能不是这样。

您可以尝试如何解决您的问题

  • 您已经在使用classes 和ids,所以您不妨尝试将选择器策略更改为CSS 选择器。至少对于失败的元素
  • 尝试更新您的 xpaths 选择器,使其不那么复杂(如果可能的话)
  • 切换到更现代的浏览器:Chrome、Firefox、Edge,如果可能的话(但我怀疑有人会出于纯粹的选择而使用 IE)

【讨论】:

  • IE是11.1845版本
  • 不,我们使用 IE 并不是出于纯粹的选择。我们大约 80% 左右的项目在 Chrome 中运行,但有些项目出于某种原因必须在 IE 中运行(使用 ColdFusion 或其他一些原因)。相信我,如果它使用 Chrome,我会喜欢的。但我确实将页面源写入文件,删除所有 然后用 chrome 编辑。如果 xpath 无效,它将一直失败。因此,在这种情况下,听起来 find 命令可能返回的不是元素吗?但是“//div”应该总是可以工作的,它也有同样的问题。
  • 忘了提一件事。以下错误是“[对象错误]”
【解决方案2】:

NoSuchElementException

NoSuchElementExceptionWebDriver.findElement(By by)WebElement.findElement(By by) 方法在没有找到匹配元素时抛出的异常。

你可以找到几个详细的讨论 NoSuchElementException 在:


InvalidSelectorException

InvalidSelectorException 是使用选择器时抛出的异常 no element can be identify 并且如前所述是NoSuchElementException的直接已知子类。

您可以在以下位置找到一些相关的详细讨论:


Internet Explorer 奇遇

然而,在之前的十几次讨论中,支持 的用户表示它更具可读性和更快,尤其是在与 运行时。

您可以在Why should I ever use CSS selectors as opposed to XPath for automated testing?找到详细讨论


这个用例

这行代码……

driver.findElement(By.xpath("//div[contains(@id, 'stage')]/div[contains(@class, 'tabMenu')]/span[contains(@class, 'menuItem') and contains(text(),'Employer')]"))

...看起来很完美。

但是,根据最佳实践,我建议避免混淆 contains()text()。您可以按如下方式使用它们中的任何一个:

  • 使用contains()

    driver.findElement(By.xpath("//div[contains(@id, 'stage')]/div[contains(@class, 'tabMenu')]/span[contains(@class, 'menuItem') and contains(.,'Employer')]"))
    
  • 使用text()

    driver.findElement(By.xpath("//div[contains(@id, 'stage')]/div[contains(@class, 'tabMenu')]/span[contains(@class, 'menuItem') and text()='Employer']"))
    

【讨论】:

  • 是的,但是每次都会失败,而不仅仅是 10%
  • 如果我捕捉到异常,刷新页面,接受警报,然后将驱动程序设置为页面,似乎可行。
猜你喜欢
  • 2013-10-30
  • 2015-04-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-20
  • 2021-12-25
  • 2020-05-10
  • 2014-09-20
相关资源
最近更新 更多