【发布时间】: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