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 fordocument.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:
- Alter your selector to be class or ID based (easiest)
- 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
How to Use 'Subprocess' Command With Pipes
Linux Command-Line Call Not Returning What It Should from Os.System
After Anaconda Installation, Conda Command Fails With "Importerror: No Module Named Conda.Cli"
Webdriverwait Not Working as Expected
Input() Error - Nameerror: Name '...' Is Not Defined
Error: Unable to Find Vcvarsall.Bat
Best Way to Structure a Tkinter Application
How to Make a Class Json Serializable
Faster Version of 'Pygame.Event.Get()'. Why Are Events Being Missed and Why Are the Events Delayed
Modifying List While Iterating
Imagemagick Not Authorized to Convert Pdf to an Image
Tkinter.Photoimage Doesn't Not Support Png Image
How to Open a File With the Standard Application
How to Iterate Over Rows in a Dataframe in Pandas
Fastest Way to List All Primes Below N
How to Add Sequential Counter Column on Groups Using Pandas Groupby