How to handle Stale Element Reference exception in Selenium Webdriver

Test Automation

In this post we will see how to handle how to handle stale element reference exception. First of all we should understand what is stale element –

Stale Element means an old element or no longer available element. Assume there is an element that is found on a web page referenced as a WebElement in WebDriver.

Possible causes for an stale element exception-

1 -The element has been deleted entirely.

2- The element is no longer attached to the DOM.

How to fix or handle it

Solution 1

Most easy easy out to handle this is to refresh the page, On refreshing it, most of the time driver found the element, But its not the perfect solution-


Solution 2-

The second option you can try is to place your driver interaction command inside some loop and to repeating that for a couple of times, 2-3 times, refer below code.

for(int i=0;i<=3;i++) { try{ driver.findElement("Property").click(); break; } } catch(Exception e) { System.out.println("Exception-Element not found"); } }

however this method is also but hardcoded and not generic, it can also fail, if even after 3 attempt element is not found however its best to use third option which is placing Do-While loop .

Solution 3-

Use a do-While loop on an element which is causing stale element exception-Initialize a counter with Zero and iterate and identify webelement, once its enabled and displayed perform action and exit from loop.

int Counter=0

String str ;

if(driver.findelement( && driver.findelement( )
catch(Exception ex)

while(Counter == 0)

Using a do-while loop is most generic and perfect solution for such stale element exceptions, it sould be able to find element even if it appear a bit late after loading a web page.

No Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

Triggering SoapUI from selenium

The software industry has adopted Service Oriented Architecture-SOA, which consist of Microservices consist of SOAP and REST. Selenium is an API which helps in Web automation, Selenium does not help in API testing although SoapUI scripts can be invoked from selenium/Java classes. Using below 3 steps SOAPUI project can be …

Selenium Basic
Step by step selenium – step 3, what is findelement method

Abstract- So far, we have learned how to launch a web browser, entering values in a text box and clicking button. In this post, we will learn about findelement method. Test automation is all about get and set, which means get from web and set into web. Talking more specifically- …

Test Automation
What is ITestListner interface and its Use in Selenium

There are several interfaces available in TestNG that allow you to modify TestNG’s behavior or to give test scripts additional strength. These interfaces are broadly called “TestNG Listeners”. Here are a few listeners which are commonly used: 1- IAnnotationTransformer 2- IAnnotationTransformer2 3- IHookable 4- IInvokedMethodListener 5- IMethodInterceptor 6- IReporter 7- …