0

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)
Aleks
  • 61
  • 2
  • 8
  • This sounds like an [X-Y problem](http://xyproblem.info/). Instead of asking for help with your solution to the problem, edit your question and ask about the actual problem. What are you trying to do? – undetected Selenium Jan 14 '20 at 10:23
  • @DebanjanB is the last portion I add better? Should i exclude the specific xpaths in the error message? I just wanted to mention that I am working in Salesforce before I receive some messages saying to use By.id or CSS selectors – Aleks Jan 14 '20 at 10:31
  • 1
    This seems like a really overly complicated XPath. Can you post some of the relevant HTML and describe what you are trying to locate? Maybe we can come up with a better locator that will work and make your life a LOT easier... :) – JeffC Jan 14 '20 at 18:08
  • @JeffC If you are able to make it any less complicated I would love to receive some tips. I am searching for multiple fields (labels and values) in one page. Unfortunally the formatting of the HTML file is shitty but that was the maximum I could do. https://pastebin.com/hHUDqn6D I copied one
    above my searched one to have the siblings of my div included. Basically what i am searching for are field labels (mostly ) and field values (,,
    – Aleks Jan 15 '20 at 10:39
  • I see the pastebin... give me a couple examples of what you are looking for, e.g. the dollar amount next to "Total:" or whatever. – JeffC Jan 15 '20 at 14:28
  • Well basically i am reading every field label and field value of that salesforce object which you can see in the screenshot: https://pasteboard.co/IQ6D9nL.png – Aleks Jan 15 '20 at 16:55
  • I am doing that to verify our middleware is encrypting/decrypting the values correctly thus I am building a map of field labels and their values and compare the resulting list to a JSON file. – Aleks Jan 15 '20 at 17:01

1 Answers1

1

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.
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • 1
    I did not downvote but upvoted just now because of the information regarding the versions (so you are up to 0 again). Unfortunally the upgrade of the chromedriver to v79 did not fix the issue. I am getting the exact same error message as above. – Aleks Jan 14 '20 at 11:08
  • @Aleks Can you update the question with a fresh set of error trace/logs so we can have a relook please? – undetected Selenium Jan 14 '20 at 11:11
  • @Aleks Checkout the answer update and let me know the status. – undetected Selenium Jan 14 '20 at 11:28
  • Unfortunally there are no excessive quotes anywhere to be found. The bracket '(' at the above mentioned span is implying the coming group of span | a | lightning-formatted-text | ... – Aleks Jan 14 '20 at 11:42
  • Can you update the question with the text based relevant HTML? – undetected Selenium Jan 14 '20 at 13:02
  • It is not site specific. If i go to https://www.google.com and try to get the web elements with the following xpath "//div//(span|a)" it throws the same error. – Aleks Jan 14 '20 at 14:43
  • 2
    Version mismatch is not the issue or the browser would not have even navigated to the page at the start of the test. The problem is clearly stated in the error message, "... is not a valid XPath expression". – JeffC Jan 14 '20 at 18:06