Syntaxerror: Failed to Execute 'Evaluate' on 'Document': the String '//Img[Contains('1236548597')]' Is Not a Valid Xpath Expression

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

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

SyntaxError: Failed to execute 'evaluate' on 'Document' error passing parameter as xpath?

This error message...

SyntaxError: Failed to execute 'evaluate' on 'Document': The string '"//img[@alt='zoom logo colour long']")' is not a valid XPath expression.

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

Seems you were almost there. You need to remove the two " characters and the extra ) to make it a valid xpath expression as follows:

<parameter name="redirectButton" value="//img[@alt='zoom logo colour long']" />


References

You can find a couple of relevant detailed discussions in:

  • SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//img[contains('1236548597')]' is not a valid XPath expression
  • selenium - Failed to execute 'evaluate' on 'Document': The string is not a valid XPath expression
  • Python Selenium SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//div contains(@class, 'product-card__subtitle')

selenium - Failed to execute 'evaluate' on 'Document': The string is not a valid XPath expression

This error message...

SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//span[contains(@class, 'md_countryName_fdxiah8' and text(), 'Colombia')]' is not a valid XPath expression.

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

Seems you were pretty close. You can use either of the following Locator Strategies:

  • Using xpath 1:

    country = countries.find_element_by_xpath("//span[contains(@class, 'md_countryName_fdxiah8') and text()='Colombia']")
  • Using xpath 2:

    country = countries.find_element_by_xpath("//span[contains(@class, 'md_countryName_fdxiah8') and contains(., 'Colombia')]")

Here you can find a relevant discussion on SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//img[contains('1236548597')]' is not a valid XPath expression


Update

To overcome the element not visible error you need to induce WebDriverWait for visibility_of_element_located() and you can use either of the following Locator Strategy:

element = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//span[contains(@class, 'md_countryName_fdxiah8') and text()='Colombia']")))

SyntaxError: Failed to execute 'evaluate' on 'Document': The string 'xpath' is not a valid XPath expression

If you look at the XPath in the error message, you will probably see what the issue is. Your text isn't surrounded by quotes as is required, e.g.

td[@area-label=November 2025]

should be

td[@area-label="November 2025"]

To fix this, you need to adjust your line of code to

dp_month = driver.find_element_by_xpath('//*/td[@aria-label="'+month_label+'"]/div[contains(text(),"'+ x_month +'")]')

Python Selenium SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//div[contains(@class, 'product-card__subtitle')

This error message...

SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//div[contains(@class, 'product-card__subtitle')

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



Solution

To locate the desired elements you can use the following xpath based Locator Strategies:

  • Women's Shoe:

    driver.find_element_by_xpath("//div[@class='product-card__subtitle' and contains(., \"Women's Shoe\")]")
  • Men's Shoe:

    driver.find_element_by_xpath("//div[@class='product-card__subtitle' and  contains(., \"Men's Shoe\")]")


References

You can find a couple of relevant detailed discussions in:

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

SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//div[contains(., 'Medium')' is not a valid XPath expression error using Selenium

This error message...

invalid selector: Unable to locate an element with the xpath expression //div[contains(., 'Medium') because of the following error:
SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//div[contains(., 'Medium')' is not a valid XPath expression.

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


You were so close. You missed the closing ] while constructing the xpath.

Effectively your line of code will be:

IWebElement element = driver.FindElement(By.XPath("//div[contains(., 'Medium')]"));

References

You can find a couple of related relevant discussions in:

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

Error when trying to to get html element using selenium

This error message...

selenium.common.exceptions.InvalidSelectorException: Message: invalid selector: Unable to locate an element with the xpath expression //input[@data-testid="SearchBox_Search_Input""] because of the following error:
SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//input[@data-testid="SearchBox_Search_Input""]' is not a valid XPath expression.

...implies that the xpath which you have used wasn't a valid xpath expression.

The error was observed for the xpath as:

//input[@data-testid="SearchBox_Search_Input""]

is different from the xpath you have provided as code trials.

However the xpath with in your code trial is a valid xpath and won't raise this error.


Ideally, to click on the element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:

  • Using CSS_SELECTOR:

    search_input = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[aria-label='Search query']")))
  • Using XPATH:

    search_input = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@aria-label='Search query']")))
  • Note: You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC


References

You can find a couple of relevant detailed discussions in:

  • SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//img[contains('1236548597')]' is not a valid XPath expression
  • selenium - Failed to execute 'evaluate' on 'Document': The string is not a valid XPath expression
  • Python Selenium SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//div'


Related Topics



Leave a reply



Submit