This section describes why you might be seeing the
InvalidSelectorError (or similar).
The CSS or xPath selector you are trying to use...
- has invalid characters or
- has an invalid query
Common Examples of the problem
A CSS selector with the following query:
* >>> #canvas div.bwu-datagrid-row div.bwu-datagrid-cell.l0
is invalid because:
Unexpected token '>' at column 4
No Such Element
This section describes why you might be seeing the NoSuchElementException (or similar).
- The element may not be present.
- Double check your locator by using the Developer Tools in your favourite browser
Stale Element Reference Exception
You have probably been directed to this page because you've
StaleElementReferenceException in your tests.
A stale element reference exception is thrown in one of two cases, the first being more common than the second:
- The element has been deleted entirely.
- The element is no longer attached to the DOM.
The Element has been deleted
The most frequent cause of this is that page that the element was part of has been refreshed, or the user has navigated away to another page. A less common, but still common cause is where a JS library has deleted an element and replaced it with one with the same ID or attributes. In this case, although the replacement elements may look identical they are different; the driver has no way to determine that the replacements are actually what's expected.
If the element has been replaced with an identical one, a useful strategy is to look up the element again. If you do this automatically, be aware that you may well be opening your tests to a race condition and potential flakiness. For example, given the code:
WebElement element = driver.findElement(By.id("example")); String text = element.getText();
element.getText returns before the element is removed from the DOM
you'll get one result. If, however, the element is removed from the DOM and your code
does an automatic lookup for the element again before
different result may be returned.
Should you wish to head down this route, the simplest hook point is to call setElementConverter .
The Element is not Attached to the DOM
A common technique used for simulating a tabbed UI in a web app is to prepare DIVs for
each tab, but only attach one at a time, storing the rest in variables. In this case,
it's entirely possible that your code might have a reference to an element that is no
longer attached to the DOM (that is, that has an ancestor which is
If WebDriver throws a stale element exception in this case, even though the element still exists, the reference is lost. You should discard the current reference you hold and replace it, possibly by locating the element again once it is attached to the DOM.
- The element changes type, but keeps the same locator sematics (JQuery and others)
The element changes type, but keeps the same locator sematics (JQuery and others)
Watermarked fields in JQuery change from a regular input to a password field as they get focus. The first end-user key press arrives in the password variant. Refer to pwField2 in this example.