Selenium.Common.Exceptions.Invalidselectorexception With "Span:Contains('String')"

selenium.common.exceptions.InvalidSelectorException with span:contains('string')

Using css_selector to locate element by text is not supported in Selenium (although it will work in the developer tools console). The only possibility is xpath

element = "//span[contains(text(), 'Control panel')]"
my_driver.find_element_by_xpath(element)

Edit: a comment by @FlorentB:

A css selector is not supported by the console either, but JQuery supports it. The $('...') from the console is a shorthand for document.querySelector which is generally overridden with JQuery when the page has it.

InvalidSelectorException: invalid selector: Unable to locate an element with the xpath expression contains(text() error message

Below line of code worked:

findByElement.FindByXPath("//*[./text()='Submitted']").Click();

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

Not a valid XPath expression trying to use div aria label contains

There's a typo in given xpath, which is invalid due to missing parenthesis. The following should work:

unfollowuser = wait.until(EC.element_to_be_clickable(
(By.XPATH, "//div[contains(@aria-label, 'Following') and @role='button']")))

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

Selenium/Python Unable to use `:contains()` in CSS_SELECTOR

The selector :contains('text') is a jQuery selector, not a valid CSS selector like Selenium is expecting. I'm assuming the reason it works on the page via Chrome's DevTools console is because the page has jQuery defined on it.

Unfortunately, I do not believe you can directly select an element via its text using a CSS selector (link).

You have two options as far as I can see:

  1. Alter your selector to be class or ID based (easiest)
  2. Create a Selenium utility to run a JS script that uses this jQuery selector; e.g. execute_script("jQuery(" + id + ":contains('" + text + "')", id, text)

selenium - the string is not a valid xpath expression

Unfortunately the tutorial site you are taking your examples from has been mangled by some too-clever-by-half word processing software so that ASCII typewriter quotes (' and ") have been turned into typographical quotes (“…”), (‘…’). XPath needs the ASCII typewriter variety.

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


Related Topics



Leave a reply



Submit