Test Exceptions

This page is created to be able to reproduce the most common Selenium Exceptions. Follow test cases below to see exactly how to get: ElementNotInteractableException, ElementNotVisibleException, NoSuchElementException and StaleElementReferenceException. If you want to learn how to deal with these exceptions in your tests, check out my Selenium WebDriver with Java for beginners program.

Create list of your favorite foods

Push “Add” button to add another row

Row 2 was added

Test case 1: NoSuchElementException
  1. Open page
  2. Click Add button
  3. Verify Row 2 input field is displayed

Row 2 doesn’t appear immediately. This test will fail with org.openqa.selenium.NoSuchElementException without proper wait


Test case 2: ElementNotInteractableException
  1. Open page
  2. Click Add button
  3. Wait for the second row to load
  4. Type text into the second input field
  5. Push Save button using locator By.name(“Save”)
  6. Verify text saved

This page contains two elements with attribute name=”Save”.
The first one is invisible. So when we are trying to click on the invisible element, we get ElementNotInteractableException.

The same action used to throw ElementNotVisibleException, but now it throws a different exception (not sure if it’s a bug in Selenium or a feature)


Test case 3: InvalidElementStateException
  1. Open page
  2. Clear input field
  3. Type text into the input field
  4. Verify text changed

The input field is disabled. Trying to clear the disabled field will throw InvalidElementStateException. We need to enable editing of the input field first by clicking the Edit button.

If we try to type text into the disabled input field, we will get ElementNotInteractableException, as in Test case 2.


Test case 4: StaleElementReferenceException
  1. Open page
  2. Find the instructions text element
  3. Push add button
  4. Verify instruction text element is no longer displayed

The instructions element is removed from the page when the second row is added. That’s why we can no longer interact with it. Otherwise, we will see StaleElementReferenceException.


Test case 5: TimeoutException
  1. Open page
  2. Click Add button
  3. Wait for 3 seconds for the second input field to be displayed
  4. Verify second input field is displayed

The second row shows up after about 5 seconds, so a 3-second timeout is not enough. That’s why we will get TimeoutException while executing steps in the above test case.