Aleks
Aleks

Reputation: 61

invalid selector: Unable to locate an element with the xpath expression error with ChromeDriver and Chrome using Selenium

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

Answers (1)

undetected Selenium
undetected Selenium

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 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:

  • You are using chromedriver=2.41
  • Release Notes of chromedriver=2.41 clearly mentions the following :

Supports Chrome v67-69

  • You are using chrome=79.0
  • Release Notes of ChromeDriver v79.0 clearly mentions the following :

Supports Chrome version 79

  • Your Selenium Client version is unknown to us.

So there is a clear mismatch between the ChromeDriver v2.41 and the Chrome Browser v79.0


Solution

Ensure that:

  • Selenium is upgraded to current levels Version 3.141.59.
  • ChromeDriver is updated to current ChromeDriver v79.0.3945.36 level.
  • Chrome is updated to current Chrome Version 79.0 level. (as per ChromeDriver v79.0 release notes)
  • Clean your Project Workspace through your IDE and Rebuild your project with required dependencies only.
  • (WindowsOS only) Use CCleaner tool to wipe off all the OS chores before and after the execution of your Test Suite.
  • (LinuxOS only) Free Up and Release the Unused/Cached Memory in Ubuntu/Linux Mint before and after the execution of your Test Suite.
  • If your base Web Client version is too old, then uninstall it through Revo Uninstaller and install a recent GA and released version of Web Client.
  • Take a System Reboot.
  • Execute your @Test as non-root user.
  • Always invoke driver.quit() within tearDown(){} method to close & destroy the WebDriver and Web Client instances gracefully.

Upvotes: 1

Related Questions