Selenium: InvalidSelectorException: invalid selector
Last verified
selenium.common.exceptions.InvalidSelectorException: Message: invalid selector: An invalid or illegal selector was specified; For documentation on this error, please visit: https://www.selenium.dev/documentation/webdriver/troubleshooting/errors#invalidselectorexceptionWhat it means
The driver could not use your locator because the selector itself is malformed or produces something that is not an element. It is a syntax problem, not a timing problem, so waiting longer will never fix it. Browsers report two main variants. For CSS: invalid selector: An invalid or illegal selector was specified. For XPath, a message that quotes your expression and says it is not a valid XPath expression, or one saying the result is [object Text] or [object Attr] and should be an element.
The XPath result variant is common in scraping code. Expressions ending in /text() or /@href are valid XPath, but WebDriver locators must return elements. Select the element and read .text or get_attribute('href') in code instead.
What is not wrong: the page. The same selector fails on every page.
What it looks like
from selenium.webdriver.common.by import By
# Invalid as a locator: returns attribute nodes
# driver.find_elements(By.XPATH, "//a[@class='product']/@href")
links = driver.find_elements(By.XPATH, "//a[@class='product']")
hrefs = [a.get_attribute("href") for a in links]
# Compound classes: use CSS, not CLASS_NAME
button = driver.find_element(By.CSS_SELECTOR, ".btn.btn-primary")Why it happens
- Unbalanced brackets or quotes in an XPath such as //div[@class='x'.
- XPath ending in /text() or /@attribute, which returns a text or attribute node, not an element.
- Using By.CLASS_NAME with a compound class string containing spaces.
- Passing an XPath to By.CSS_SELECTOR or CSS to By.XPATH.
- CSS selectors with unescaped special characters, such as IDs that start with a digit or contain a colon.
How to fix it
- Test the selector in DevTools: document.querySelectorAll('...') for CSS, $x("...") for XPath.
- Remove /text() or /@attr from the XPath and read the value in code: el.text or el.get_attribute('href').
- For multiple classes, use CSS: By.CSS_SELECTOR, '.btn.btn-primary' instead of By.CLASS_NAME with spaces.
- Match the By strategy to the selector syntax.
- Escape special characters in CSS, for example #\31 23 for id='123' or [id='a:b'] for colons.
How to stop it recurring
Validate selectors in DevTools before putting them in code, and keep locators in one place (page objects) so they are easy to test. Prefer short CSS selectors on stable attributes over long XPath expressions; reserve XPath for text matching and axis navigation that CSS cannot do.
Official documentation: www.selenium.dev/documentation/webdriver/troubleshooting/errors/#invalidselectorexception. See all Selenium error messages.
Related errors
Definitions
- SeleniumSelenium is an open-source browser automation project, best known for Selenium WebDriver, a library that contr
- ChromeDriverChromeDriver is the Chromium project's standalone server that implements the W3C WebDriver standard for Chrome
- CSS SelectorA CSS selector is a pattern used to identify and target specific HTML elements on a web page, widely used in b
- Headless ChromeHeadless Chrome is Google Chrome running without a visible window. It renders pages and runs JavaScript like n
- IframeAn iframe (inline frame) is an HTML element that embeds a separate web page within a parent page, creating an
- Web ElementA web element is any individual component rendered on a web page — buttons, links, input fields, images, text
Quick answers
What does "InvalidSelectorException: invalid selector" mean in Selenium?
The driver could not use your locator because the selector itself is malformed or produces something that is not an element. It is a syntax problem, not a timing problem, so waiting longer will never fix it. Browsers report two main variants. For CSS: invalid selector: An invalid or illegal selector was specified .
How do I fix "InvalidSelectorException: invalid selector"?
1. Test the selector in DevTools: document.querySelectorAll('...') for CSS, $x("...") for XPath. 2. Remove /text() or /@attr from the XPath and read the value in code: el.text or el.get_attribute('href'). 3. For multiple classes, use CSS: By.CSS_SELECTOR, '.btn.btn-primary' instead of By.CLASS_NAME with spaces. 4. Match the By strategy to the selector syntax. 5. Escape special characters in CSS, for example #\31 23 for id='123' or [id='a:b'] for colons.
Run browser automation you can watch and take over
Autonoly runs each job in a real cloud Chromium browser driven by Playwright. Timeouts and detached elements still happen; the difference is that you can watch the run live, see the step that failed, and take control of the browser to finish it.
Also see Live browser control.