• Skip to main content
  • Skip to primary sidebar
  • Skip to footer
  • Home
  • Programming Languages
    • Java Tutorials
    • Python Tutorials
    • JavaScript Tutorials
  • Automation Tools and Different Tools
    • Web Automation
      • Selenium with Java
        • Selenium Basic
        • Selenium Advance
        • Selenium Realtime
        • Framework
        • Selenium Interview
        • Selenium Videos
        • Selenium with Docker
      • Selenium with Python
      • WebdriverIO
        • Selenium Webdriver C# Tutorial
      • Cypress
      • Playwright
    • TestNG
    • Cucumber
    • Mobile Automation
      • Appium
    • API Testing
      • Postman
      • Rest Assured
      • SOAPUI
    • testRigor
    • Katalon
    • TestProject
    • Serenity BDD
    • Gradle- Build Tool
    • RPA-UiPath
    • Protractor
    • Windows Automation
  • Automation For Manual Testers
  • Services
  • Online Training
  • Contact us
  • About me
  • Follow us
    • Linkedin
    • Facebook Group
    • Facebook Page
    • Instagram

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

July 10, 2015 by Mukesh Otwani 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.

 

Filed Under: Advance Selenium Tagged With: Stale Element Reference Exception in Selenium Webdriver

Reader Interactions

Comments

  1. Gaurav Khurana says

    December 1, 2016 at 9:08 AM

    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

    Reply
    • Mukesh Otwani says

      December 6, 2016 at 2:17 PM

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

      Reply
    • Sudha says

      February 13, 2017 at 4:01 PM

      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?

      Reply
  2. shivangi says

    October 10, 2016 at 2:07 PM

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

    Reply
    • Mukesh Otwani says

      October 10, 2016 at 2:53 PM

      Cheers Shivangi

      Reply
  3. Reetanshu Kumar says

    September 30, 2016 at 11:59 PM

    Good article, really a wonderful way of explaining things

    Reply
    • Mukesh Otwani says

      October 3, 2016 at 4:31 PM

      Thanks Reetanshu

      Reply
  4. ashish says

    August 7, 2016 at 1:46 AM

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

    Reply
    • Mukesh Otwani says

      August 18, 2016 at 10:58 PM

      Hi Ashish,

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

      Reply
  5. Jupiter says

    May 17, 2016 at 2:57 AM

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

    Reply
    • Mukesh Otwani says

      May 17, 2016 at 12:13 PM

      Thanks Mate

      Reply
  6. autotester says

    December 22, 2015 at 5:21 PM

    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

    Reply
    • Mukesh Otwani says

      December 23, 2015 at 12:31 AM

      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

      Reply
  7. Satya says

    December 22, 2015 at 11:22 AM

    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 ?

    Reply
    • Mukesh Otwani says

      December 22, 2015 at 11:56 AM

      Yes please try solution 2

      Reply
  8. Srinivas says

    December 2, 2015 at 8:02 PM

    Really its very helpful post.

    Reply
    • Mukesh Otwani says

      December 2, 2015 at 9:35 PM

      Hi Srinivas,

      Thanks keep visiting..

      Reply
  9. rajkrish06 says

    September 30, 2015 at 10:56 AM

    Solution 2 worked for me

    Reply
    • Mukesh Otwani says

      September 30, 2015 at 8:27 PM

      Great Raj Cheers 🙂

      Reply

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.

Primary Sidebar

Free Selenium Videos

https://www.youtube.com/watch?v=w_iPCT1ETO4

Search topic

Top Posts & Pages

  • Selenium Webdriver tutorial for beginners
  • How To Fix Eclipse Autocomplete Or Code Suggestion In Eclipse
  • Selenium Webdriver C# Tutorial
  • WHAT ARE YOUR EXPECTATIONS FROM US?

Stay connected via Facebook

Stay connected via Facebook

Archives

Footer

Categories

Recent Post

  • API Testing Using Postman And RestAssured
  • Disable Personalise Your Web Experience Microsoft Edge Prompt In Selenium
  • How To Fix Error: No tests found In Playwright
  • How To Fix Eclipse Autocomplete Or Code Suggestion In Eclipse
  • Best and easy way to Group test cases in selenium

Top Posts & Pages

  • Selenium Webdriver tutorial for beginners
  • How To Fix Eclipse Autocomplete Or Code Suggestion In Eclipse
  • Selenium Webdriver C# Tutorial
  • WHAT ARE YOUR EXPECTATIONS FROM US?