[フレーム]

Automation

Selenium WebDriver tutorial Step by Step

You are here: Home / Advance Selenium / How to solve Stale Element Reference exception in Selenium Webdriver

How to solve Stale Element Reference exception in Selenium Webdriver

by 19 Comments

[画像: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.

Reader Interactions

Comments

  1. 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

    • 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. 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

  3. 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 ?

Leave a Reply Cancel 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.

AltStyle によって変換されたページ (->オリジナル) /