NoSuchElementException in Selenium


This is a page describing how to deal with Selenium’s NoSuchElementException in Java.

NoSuchElementException is thrown by findElement() function to indicate that the element can't be found provided the locator.

Main reasons for the exception

The most common reasons for the exception include:

  • Change in HTML/XML structure: the same element now has a different locator compare to what it used to have before. This is very common for actively developed projects.
  • Element is no longer on the page/screen: the element used to be on the current page but no longer can be found. This can indicate an issue on the current step as well as an issue on a previous step leading to the situation where current page/state is different from the expected one.

How to deal with the NoSuchElementException exception

As a first step you'd want to find out what kind of issue you are dealing with? Is element still there and can't be found or is it no longer present on the page. To do it you can setup Selenium to not to close the browser after the test failure (the default setting) and run the test. Once you get the exception, you can inspect the page that is open in the browser. If element if still there you can inspect it and get the new locator. If it is not there you can assess if there is an issue on the current step or on one of the previous steps.

Join the next wave of functional testing now.
A testRigor specialist will walk you through our platform with a custom demo.