SyntaxError: Failed to execute ‘evaluate’ on ‘Document’: The string ‘//img[contains(‘1236548597′)]’ is not a valid XPath expression

I am getting syntax error:

SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//img[contains('1236548597')]' is not a valid XPath expression.

My code:

enter_chat = driver.find_element_by_xpath("//img[contains('1236548597')]")
enter_chat.click()

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

This error message…

SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//img[contains('1236548597')]' is not a valid XPath expression.

…implies that the XPath which you have used was not a valid XPath expression.

In your code trial the predicate with contains() function has an issue.

  • contains() accepts two parameters. The first parameter is always the attribute to be tested and the second parameter is the value to lookout for. The first parameter should have been the attribute which contains the value 1236548597:
    enter_chat = driver.find_element_by_xpath("//img[contains(@<attributeName>,'1236548597')]")

It would be tough to construct an answer without the relevant HTML. However two possible solutions are as follows:

  • If the tag is a <a> tag, containing an img attribute containing the value 1236548597:
    enter_chat = driver.find_element_by_xpath("//a[contains(@img,'1236548597')]")
  • If the tag is a <img> tag, containing an src attribute containing the value 1236548597:
    enter_chat = driver.find_element_by_xpath("//img[contains(@src,'1236548597')]")

Method 2

It should be,

enter_chat = driver.find_element_by_xpath("//img[contains(text(),'1236548597')]")
enter_chat.click()

if "//img[contains(text(),'1236548597')]" not work, 
try "//img[contains(.,'1236548597')]"

You have missed text() function.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x