InvalidSelectorException in Selenium
Updated 2026-11-13 · 2 min read
The locator expression cannot be interpreted for its strategy. Repair syntax and CSS/XPath mix-ups before investigating page state or timing.
Recognise the error
The official Java API names this exception:
org.openqa.selenium.InvalidSelectorException
This is the documented exception identifier, not a reproduced stack trace. Driver-specific wording and diagnostic details vary. Keep the full message from your own run; no browser error was reproduced for this page. Java fragments below assume an existing Selenium 4 WebDriver driver and standard Selenium imports. They were checked against the APIs but not compiled or run here.
The selector could not be interpreted for the selected locator strategy. Read the selector and strategy together before investigating timing.
The WebDriver specification defines the corresponding protocol error string as:
invalid selector
1. XPath was passed as CSS, or CSS as XPath
Use the strategy that matches the expression. These alternatives both target an element with the ID submit:
By css = By.cssSelector("#submit");
By xpath = By.xpath("//*[@id='submit']");
driver.findElement(css).click();
Do not pass //*[@id='submit'] to By.cssSelector. A wait around that lookup repeats an invalid request.
2. A bracket or quote is missing
Reduce the expression to the shortest useful locator. Check it against representative HTML in the XPath and CSS tester, then check the live page in DevTools. A syntax-only check does not prove uniqueness.
By email = By.cssSelector("input[name='email']");
driver.findElement(email).sendKeys("[email protected]");
Prefer a stable ID or test attribute when test data would otherwise need complex quoting. Do not build XPath by joining arbitrary user text into quotes.
3. The expression uses another tool's selector language
A Playwright text selector is not a Selenium CSS selector. Likewise, XPath functions must be supported by the browser's evaluator; do not assume every XPath feature in an editor works in WebDriver. Rewrite the locator with ordinary browser-supported CSS or XPath.
By save = By.xpath("//button[normalize-space(.)='Save']");
driver.findElement(save).click();
Prevention and interview answer
Store the locator strategy with the locator text, validate new selectors against the rendered page, and assert the intended match rather than choosing an arbitrary first result. “I repair the expression or strategy first. If valid syntax returns no nodes, I move on to page state and timing.”
That next investigation is NoSuchElementException. A valid Playwright locator with too many matches is instead a strict mode violation.
Quick answers
Will an explicit wait repair an invalid selector?
No. Waiting does not change selector syntax.
Is a valid selector with no matches invalid?
No. That normally leads to a missing-element result or a wait timeout instead.
Sources
Want a project you can show in interviews?
The portfolio project kit walks you through testing a deliberately buggy app with Selenium or Playwright and API tests, then explaining it. The app itself is free.
