Rabe
Rabe

Reputation: 87

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

Upvotes: 2

Views: 25836

Answers (2)

undetected Selenium
undetected Selenium

Reputation: 193108

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

Upvotes: 3

Ishita Shah
Ishita Shah

Reputation: 4035

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.

Upvotes: -1

Related Questions