【问题标题】:invalid selector: Unable to locate an element with the xpath expression error with ChromeDriver and Chrome using Selenium无效的选择器:无法使用 ChromeDriver 和 Chrome 使用 Selenium 定位具有 xpath 表达式错误的元素
【发布时间】:2020-05-01 01:23:46
【问题描述】:

我目前面临 Selenium 和 findElements(By.xpath()) 的问题。

目前我在 Salesforce 上实现自动化(动态 ID,我们使用 text(),所以我们不能使用 CSS 选择器),而我的 XpathFinder(Chrome 插件)能够处理 Xpath Selenium 时会抛出以下错误:

    invalid selector: Unable to locate an element with the xpath expression 

//div[contains(@class,'windowViewMode-normal')]//div[@class='slds-tabs_default' and not(ancestor::* 
     [contains(@class,'sidebar')])]//force-record-layout-item//(span[not(contains(@class,'assistive') 
     or @class='slds-checkbox' or contains(@class,'slds-button') or contains(@class,'test-id__field- 
     value') or contains(@class,'flex-wrap') or contains(@class,'avatar'))]|a|emailui-formatted-email- 
     wrapper//div[not(*)]|lightning-formatted-text|lightning-formatted-number[text()]|lightning- 
     formatted-name|lightning-formatted-phone[not(*)]|lightning-formatted-address[not(*)]) 
because of the following error:
    SyntaxError: Failed to execute 'evaluate' on 'Document': The string 
    '//div[contains(@class,'windowViewMode-normal')]//div[@class='slds-tabs_default' and 
    not(ancestor::*[contains(@class,'sidebar')])]//force-record-layout-                
    item//(span[not(contains(@class,'assistive') or @class='slds-checkbox' or contains(@class,'slds- 
    button') or contains(@class,'test-id__field-value') or contains(@class,'flex-wrap') or 
    contains(@class,'avatar'))]|a|emailui-formatted-email-wrapper//div[not(*)]|lightning-formatted- 
    text|lightning-formatted-number[text()]|lightning-formatted-name|lightning-formatted- 
    phone[not(*)]|lightning-formatted-address[not(*)])' 
is not a valid XPath expression.
(Session info: chrome=79.0.3945.117)
(Driver info: chromedriver=2.41.578737 (49da6702b16031c40d63e5618de03a32ff6c197e),
platform=Windows NT 10.0.17763 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 0 milliseconds

问题出在分组后

//div[contains(@class,'windowViewMode-normal')]//div[@class='slds-tabs_default' and not(ancestor::* 
 [contains(@class,'sidebar')])]//force-record-layout-item//...

如果我删除分组(第一个跨度之前的 ( 和 xpath 末尾的 ) )并在每个 | 之后添加 xpath 的该部分。 XpathFinder 和 Selenium 都可以评估 xpath。

是我的分组错误,浏览器只是巧合地能够评估这个还是 Selenium 在评估 xpath 时出错?

所以简单总结一下:

如果我尝试评估以下内容

//div//(a|b)

评估因给定错误而失败。分组做错了还是我错过了其他东西?

编辑:

将 ChromeDriver 升级到 v79 以匹配版本后出现相同的错误:

invalid selector: Unable to locate an element with the xpath expression 

(//div[contains(@class,'windowViewMode-normal')]//div[@class='slds- 
tabs_default' and not(ancestor::*[contains(@class,'sidebar')])]//force-record- 
layout-item)//(span[not(contains(@class,'assistive') or @class='slds-checkbox' 
or contains(@class,'slds-button') or contains(@class,'test-id__field-value') or 
contains(@class,'flex-wrap') or contains(@class,'avatar'))]|a|emailui- 
formatted-email-wrapper//div[not(*)]|lightning-formatted-text|lightning- 
formatted-number[text()]|lightning-formatted-name|lightning-formatted- 
phone[not(*)]|lightning-formatted-address[not(*)]) 

because of the following error:

SyntaxError: Failed to execute 'evaluate' on 'Document': The string  

'(//div[contains(@class,'windowViewMode-normal')]//div[@class='slds- 
tabs_default' and not(ancestor::*[contains(@class,'sidebar')])]//force-record- 
layout-item)//(span[not(contains(@class,'assistive') or @class='slds-checkbox' 
or contains(@class,'slds-button') or contains(@class,'test-id__field-value') or 
contains(@class,'flex-wrap') or contains(@class,'avatar'))]|a|emailui- 
formatted-email-wrapper//div[not(*)]|lightning-formatted-text|lightning- 
formatted-number[text()]|lightning-formatted-name|lightning-formatted- 
phone[not(*)]|lightning-formatted-address[not(*)])' is not a valid XPath 
expression.
(Session info: chrome=79.0.3945.117)

【问题讨论】:

  • 这听起来像是X-Y problem。与其寻求解决问题的帮助,不如编辑您的问题并询问实际问题。你想做什么?
  • @DebanjanB 是我添加的最后一部分吗?我应该在错误消息中排除特定的 xpath 吗?我只是想提一下,在收到一些说要使用 By.id 或 CSS 选择器的消息之前,我正在 Salesforce 工作
  • 这似乎是一个非常复杂的 XPath。您能否发布一些相关的 HTML 并描述您要查找的内容?也许我们可以想出一个更好的定位器,它可以让你的生活更轻松...... :)
  • @JeffC 如果你能够让它变得不那么复杂,我很乐意收到一些提示。我在一页中搜索多个字段(标签和值)。不幸的是,HTML 文件的格式很糟糕,但这是我能做的最大的事情。 pastebin.com/hHUDqn6D 我在搜索到的
    上方复制了一个
    以包含我的 div 的兄弟姐妹。基本上我要搜索的是字段标签(主要是 )和字段值(,,
  • 我看到了 pastebin... 给我几个你正在寻找的例子,例如“总计:”或其他旁边的美元金额。

标签: selenium google-chrome xpath selenium-chromedriver invalidselectorexception


【解决方案1】:

此错误消息...

invalid selector: Unable to locate an element with the xpath expression 

//div[contains(@class,'windowViewMode-normal')]...lightning-formatted-address[not(*)])' 
is not a valid XPath expression.
(Session info: chrome=79.0.3945.117)
(Driver info: chromedriver=2.41.578737 (49da6702b16031c40d63e5618de03a32ff6c197e),
platform=Windows NT 10.0.17763 x86_64) (WARNING: The server did not provide any stacktrace information)

...暗示 ChromeDriver 由于invalid selector 而无法定位元素。


如果您观察到您正在使用的 中的中间<span> 标记,则似乎存在一对额外的单引号导致InvalidSelectorException

//(span[not(contains(@class,'assistive') or @class='slds-checkbox' or contains(@class,'slds-button') or contains(@class,'test-id__field-value') or contains(@class,'flex-wrap') or contains(@class,'avatar'))]                                                                                  
  ^starting here 

删除这将解决您的invalid selector问题


但是您更大的问题是您使用的二进制文件版本之间的不兼容性,如下所示:

  • 您正在使用 chromedriver=2.41
  • chromedriver=2.41 的发行说明明确提及以下内容:

支持 Chrome v67-69

  • 您正在使用 chrome=79.0
  • ChromeDriver v79.0 的发行说明明确提及以下内容:

支持Chrome 79版

  • 您的 Selenium 客户端 版本对我们未知

所以 ChromeDriver v2.41Chrome 浏览器 v79.0

之间存在明显的不匹配

解决方案

确保:

  • Selenium 升级到当前级别 Version 3.141.59
  • ChromeDriver 已更新到当前的ChromeDriver v79.0.3945.36 级别。
  • Chrome 已更新至当前 Chrome 版本 79.0 级别。 (根据ChromeDriver v79.0 release notes
  • 清理你的项目工作区通过你的IDE重建你的项目只需要依赖。
  • 仅限 Windows 操作系统)使用 CCleaner 工具在执行您的测试套件之前和之后清除所有操作系统琐事。
  • 仅限 LinuxOSFree Up and Release the Unused/Cached Memory in Ubuntu/Linux Mint 在执行您的测试套件之前和之后。
  • 如果您的基础 Web Client 版本太旧,请通过 Revo Uninstaller 卸载它并安装最新的 GA 和发布版本的 Web Client
  • 进行系统重启
  • 非root用户身份执行@Test
  • 始终在 tearDown(){} 方法中调用 driver.quit() 以优雅地关闭和销毁 WebDriverWeb Client 实例。

【讨论】:

  • 由于有关版本的信息,我刚刚没有投反对票,而是投了赞成票(所以你再次达到 0)。不幸的是,将 chromedriver 升级到 v79 并没有解决这个问题。我收到与上面完全相同的错误消息。
  • @Aleks 您能否用一组新的错误跟踪/日志更新问题,以便我们重新查看?
  • @Aleks 查看答案更新并告诉我状态。
  • 不幸的是,在任何地方都找不到过多的引号。上述 span 中的括号 '(' 表示即将到来的一组 span | a | 闪电格式文本 | ...
  • 版本不匹配不是问题,否则浏览器甚至不会在测试开始时导航到页面。错误消息中清楚地说明了问题,“...不是有效的 XPath 表达式”。
猜你喜欢
相关资源
最近更新 更多
热门标签