Advance Selenium

How to solve Stale Element Reference exception in Selenium Webdriver

This is going to be one of the shortest posts of mine, but this post is quite important for those who are working with Selenium on the real time project because I will show you how to solve stale element reference exception in selenium webdriver.

I am sure  that you would have definitely faced StaleElemenet exception in Selenium Webdriver.

I have seen so many exception in my application and after struggling couple of hours got the solution, which I already listed. You can refer below post, which will help you in case if you will get the exception.

If you are new to Selenium then refer post to see how to handle exception.

 

Why we are getting Stale Element Reference in Selenium Webdriver

Don’t worry keep calm because oSelenium guys already aware of this exception.

Two reasons for Stale element reference.

 

stale element reference exception in selenium

Please check Selenium official documentation for the same 

1 -The element has been deleted entirely.

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

 

How to solve stale element reference exception in selenium

Solution 1

You can refresh the page and again try for the same element.

Example- If you are trying to click on link and getting the exception then try in below format

 

Driver.navigate().refersh();

Driver.findElement(By.id(“ur element id”)).click();

 

Solution 2-

Sometimes it takes the time to attach element on Dom so you can retry using for loop and try catch.

 

for(int i=0i<=2;i++)

{

  try{

     Driver.findElement(By.id()).click();

    break;

}

catch(Exception e)
{

Sysout(e.getMessage());

}

}

 

In the above program, we will try for specific element max 3 times and if the element is located in the first shot itself then it will break from for loop and come out of the loop.

 

This solution, which worked for me, I have mentioned here if you have any additional scenario, which worked for you then comment below or contact me so that we can add into solution list.

 

Hope you enjoyed above post.

 

author-avatar

About Mukesh Otwani

I am Mukesh Otwani working professional in a beautiful city Bangalore India. I completed by BE from RGPV university Bhopal. I have passion towards automation testing since couple of years I started with Selenium then I got chance to work with other tools like Maven, Ant, Git, GitHub, Jenkins, Sikuli, Selenium Builder etc.

19 thoughts on “How to solve Stale Element Reference exception in Selenium Webdriver

  1. Could you please create an example for this as for those who have not faced this problem might not be able to implement this in a program. Even if you can inform some URL where this can be tested it would be great

    1. For this I have to check as of now I dont have any scenario for this.

    2. Sudha says:

      Hi gaurav,
      yes i’ll. for eg. a page containing so many supplier records with each record has a view button. if we click the view button,record of many users will be available with again view for each users.
      The problem is while iterating the view buttons, i’m getting stale element reference exception while going back and tried to doing the same. how to solve this issue?

  2. shivangi says:

    thanks a lot. it worked in one shot , the try -catch in for loop !

  3. Reetanshu Kumar says:

    Good article, really a wonderful way of explaining things

  4. ashish says:

    Hi Mukesh, I am confused a little bit. Why using explicit or implicit wait is not one of the solutions?

    1. Hi Ashish,

      Wait does not work always. Explicit works sometime for this but implicit wait never works in this exception.

  5. Jupiter says:

    OMG! I love you men!!! THANKS!!
    I was going crazy to solve this!!

  6. autotester says:

    HI Mukesh,

    We are running a regression suite around 2000 scrips and able to get a pass percentage of 80%. out of the failed scripts most of them are failed with throwing Stale Element Reference exception. Could you please suggest some solutions for this specific scenario or can we expect these failure since we are running around 2000 scripts

    Thanks in Advance

    1. Hi Alvin,

      Automation never gives 100 ROI but still you can add below feature in your project which will run failed test cases

      https://vistasadprojects.com/mukeshotwani-blogs-v2/re-run-selenium-failed-test-cases/

      Thanks
      Mukesh

  7. Satya says:

    Hi Mukesh,

    I am also getting the above exception while selecting value from drop down. Can i solve this by using the solution no 2 ?

    1. Yes please try solution 2

  8. Srinivas says:

    Really its very helpful post.

    1. Hi Srinivas,

      Thanks keep visiting..

  9. rajkrish06 says:

    Solution 2 worked for me

    1. Great Raj Cheers 🙂

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.