Handling StalereferenceExceptions in Selenium

As the #BlogATAhon Series 1: Selenium gathers steam I thought of sharing something which every automation tester who is learning or practicing Selenium may have encountered “StaleElementReferenceException: Element not found in the cache – perhaps the page has changed since it was looked up”

Lets decipher this exception message: As the word Stale element reference suggests which means element reference has become old. And then it goes on to say its not found on cache which means a page has changed since the last instance. So a login textbox which was previously getting identified by Selenium is not getting identified now!!

So as usual when we debug and we see that the locator details have not changed i.e. id or xpath are still the same.

So this can happen when the libraries supporting those login box has changed which means the text box is same but the reference has now changed in the website without affecting the locators. Thus the reference we stored in our cache including the library reference has now become old or stale because the page has been refreshed with updated libraries.

So somehow we need to handle this situation by putting the updated element reference into the cache. This can be handled in one of the ways as below:

  1. Using a for/while loop which will find try to find the element for a certain number of times. If its not found the exception message it generates can be handled using a try catch block. If the exception is not generated then it indicates it has found the element. Lets see this example by implementing the same thing in WebDriver code.

Non-working code or Bad Code in TestNG

@Test
public void newTst() throws InterruptedException

{
//Located element and stored It’s reference In variable.

WebElement Login = driver.findElement(By.id(“login1′”));
//Used element reference variable to locate element and perform search.
Login.sendKeys(“John”);

Lets say we will get the StaleElementReferenceException here

Lets handle this with the for loop / while loop

Using for loop

for(int j=0; j<5;j++)
try {
WebElement Login=driver.findElement(By.id(“login1′”));
break;
} catch(StaleElementReferenceException e)

{
e.toString();
System.out.println(“Stale element error, trying again – ” + e.getMessage());
}
Login.sendKeys(“John”);

So in above case finding the locator will be tried for 5 times. If there is an exception message printed in the console it means the locator was not found and it will be retried again till the for loop exits or the element is found.

Similarly we can use a while loop to perform the same operation

Agile Testing Alliance is proud to present its next competition #BlogATAhon. As the name suggests #BlogATAhon is all about Blogs. #BlogATAhon is a series of blog writing competitions across various categories.
The first blogATAhon series is on Selenium – This is a community initiative to bring all Selenium enthusiasts together.

If you are into Selenium and you want to get yourself noticed by getting your blogs published on ATABlogs.. this competition if for you

For more details please visit BlogATAhon page
https://blogatahon.agiletestingalliance.org/

Leave a Reply