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()

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')]")