Reputation: 61
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)
Upvotes: 0
Views: 4270
Reputation: 193108
This error message...
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)
...implies that the ChromeDriver was unable to locate an element due to invalid selector.
If you observe the intermediate <span>
tag within the xpath you are using, there seems an extra pair of single quotes present which is causing 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
Removing this will solve your issue of invalid selector
However your bigger issue is the incompatibility between the version of the binaries you are using as follows:
Supports Chrome v67-69
Supports Chrome version 79
So there is a clear mismatch between the ChromeDriver v2.41 and the Chrome Browser v79.0
Ensure that:
@Test
as non-root user.driver.quit()
within tearDown(){}
method to close & destroy the WebDriver and Web Client instances gracefully.Upvotes: 1