I am currently facing a problem with Selenium and findElements(By.xpath()).
Currently I am automating on Salesforce (dynamic IDs and we are using text() so we cannot use CSS selectors) and while my XpathFinder (Chrome AddOn) is able to process the Xpath Selenium throws the following error:
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
The problem is in the grouping after
//div[contains(@class,'windowViewMode-normal')]//div[@class='slds-tabs_default' and not(ancestor::*
[contains(@class,'sidebar')])]//force-record-layout-item//...
If i remove the grouping (the ( before the first span and ) at the end of the xpath) and add that part of the xpath after every | the xpath can be evaluated by both XpathFinder and Selenium.
Is my grouping wrong and the browser is just coincidentlly able to evaluate this or is Selenium buggy while evaluating xpaths?
So to summerize it simply:
If i try to evaluate the following
//div//(a|b)
the evaluation failed with the given error. Is the grouping done wrong or am I missing something else?
EDIT:
After getting ChromeDriver to v79 to match the versions the same error appeared:
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)