Handling StaleElementReferenceException when WebDriver wait is not working

Jasminder Singh
GoMechanic
Published in
3 min readDec 21, 2021

In this post, I will talk about a problem that our team faced while automating a webpage for our internal website. I will discuss the test case below:-

Use Case:

1.) Open the website

2.) Enter mandatory fields (phone no. and car model)

3.) Open service listing page

4.) Select a service

5.) Checkout and place order

Looks like a very generic case for any e-commerce website and in reality is very simple too. So, while developing the test scripts for the same, we will select and perform the necessary actions on the elements in the page class and then call the functions in the test class.

However, in this case one simple line of code for selectServiceTest() failed every time as the console was giving a stale element exception on execution.

selectServiceTest(tests.RetailWebsiteOrderingTest) Time elapsed: 2.626 sec <<< FAILURE!

org.openqa.selenium.StaleElementReferenceException:

stale element reference: element is not attached to the page document

This exception simply means that the element that the script is clicking is not actually present in the current DOM. However, we have given an explicit wait and waited for the element to be visible and also added a condition to avoid the stale element exception in our code. Below is the snippet for the same.

Wait wait = new FluentWait(driver).withTimeout(Duration.ofSeconds(30)) .ignoring(StaleElementReferenceException.class).pollingEvery(Duration.ofSeconds(2));

wait.until(ExpectedConditions.visibilityOf(addToCart));

addToCart.click();

We also tried refreshing the page to reload the DOM once again with selenium’s inbuilt refresh method

driver.navigate().refresh();

However, there is a catch here, when we tried to click on the “Add to cart” button for some specific services then this code was getting executed without any error. On analyzing we found out that this exception was only coming when clicking on “Add to Cart” was opening a mini pop-up within the same page and thus the new loaded DOM did not have the “Add to Cart” button’s reference.

Before Clicking Add To Cart

After clicking Add to Cart, the Engine Oil pop-up opens, and at this point, selenium cannot find “Add to Cart” in the current DOM

Resolution

Eventually, we tweaked the code a bit for all the cases where clicking on “Add to cart” will open a mini pop-up by clicking on the category first for eg:-

1.) User lands on a page where they have to enter details

2.) User lands on a default category without actually clicking on the category.

3.) User clicks on the main category again.

4.) User clicks on “Add to cart”.

Adding Step 3 solved the problem for us as now Add to Cart was a part of the current loaded DOM.

Summary

If you are still getting StaleElementReferenceException after providing “wait” and refreshing the page, check if there is an action that is reloading all the elements of the page and then perform an action that reloads the DOM.

--

--