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

 

 

Solution 2-

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

 

 

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.

 

18 Comments

  1. shivangi
  2. Reetanshu Kumar
  3. ashish
  4. Jupiter
  5. autotester
  6. Satya
  7. Srinivas
  8. rajkrish06

Leave a Reply