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.
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.
Gaurav Khurana says
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
Mukesh Otwani says
For this I have to check as of now I dont have any scenario for this.
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?
shivangi says
thanks a lot. it worked in one shot , the try -catch in for loop !
Mukesh Otwani says
Cheers Shivangi
Reetanshu Kumar says
Good article, really a wonderful way of explaining things
Mukesh Otwani says
Thanks Reetanshu
ashish says
Hi Mukesh, I am confused a little bit. Why using explicit or implicit wait is not one of the solutions?
Mukesh Otwani says
Hi Ashish,
Wait does not work always. Explicit works sometime for this but implicit wait never works in this exception.
Jupiter says
OMG! I love you men!!! THANKS!!
I was going crazy to solve this!!
Mukesh Otwani says
Thanks Mate
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
Mukesh Otwani says
Hi Alvin,
Automation never gives 100 ROI but still you can add below feature in your project which will run failed test cases
http://learn-automation.com/re-run-selenium-failed-test-cases/
Thanks
Mukesh
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 ?
Mukesh Otwani says
Yes please try solution 2
Srinivas says
Really its very helpful post.
Mukesh Otwani says
Hi Srinivas,
Thanks keep visiting..
rajkrish06 says
Solution 2 worked for me
Mukesh Otwani says
Great Raj Cheers 🙂