Make Selenium Easy

StaleElementReferenceException – Element’s “Reference” Stales – Get New Reference Of Element

Let’s start with some lines of code :-

  1. Launch login page of a website.
  2. Locate a WebElement “EmailAddress” and store in a WebElement reference variable.
  3. Perform some actions multiple times on already located WebElement.
  4. Refresh the web page.
  5. Perform same actions on Already located WebElement EmailAddress again.

Selenium Program :-

Output :-

You will see repeating actions were performed perfectly three times but you get “Stale Element Reference” exception as soon as you refresh the page and try to perform actions on already located web element.

If you are new to this exception and read console message as “element is not attached to the page document“, the first thing you may think “Locator is changed after refresh“. You have a good habit of referring official document and you go to Selenium doc page of StaleReferenceException and you read a new thing about it as ” Indicates that a reference to an element is now “stale” — the element no longer appears on the DOM of the page“. If you don’t concentrate on first half of line, second part will make you think again the same that ” Locator is changed after refresh“.

Now if you are thinking that locator is changed, let think more on it.

  1. If locator is changed, you should have got “NoSuchElementException” not “StaleElementReferenceException“.
  2. After refresh, you are not relocating web element so there is no locator involved. If no locator is involved then how you can conclude that locator is changed.

Let’s consider a scenario :-

You are a famous celebrity and stay in a beautiful flat in a society. Many fans send you appreciation letters to your flat address. You earned a lot more and bought a new sea facing house in a new society but you forgot to update your new residential address to your fans. Fans are crazy and they are writing you letters to the old address. When postman tries to deliver those letters, postman will not able to find your address in that society. Your old address has become stale now. All letters send to old address are bouncing back to fans.

Same thing has happened above with selenium code.

When you locate a Web Element on page, it gets a reference in DOM using some identification (As ID in WebDriver APIs). DOM changed when I refreshed the page. So when I passed the already located or old reference of Web element to (updated ) DOM, it reverts saying “This person does not stay here” means “this element is not attach to the page document“. It looks at reference of element not using which locator founded this.

Let’s relocate web element again after refresh and see the output :-

Now you will not get StaleElementReferenceException.

Am I making some sense or able to connect dots? You can always send a mail to me or comment. You can share your understanding as well.

Reason for stale element reference : –

Reason is only one :- Changes in DOM but DOM may change explicitly or implicitly.

I changed DOM above using refresh but anything can change your DOM which you can observe and sometimes you can not. An Ajax calls or method calling, validation scripts anything can change your DOM.

Ways of handling stale element reference exception :-

Thread.sleep() : – No, it will not solve if not used correctly.

Many people suggest put a sleep or explicit condition, it will solve problem. In above problem you put a sleep for whatever duration and explicit wait on already located element, problem will not be solved.

Sometimes you may have located web element but internally some method call is going on which changes DOM. If you are big fan of sleep method, you can use sleep() here till execution of internal method calls is completed. But you NEVER know how much time it will take. Sometimes it may be quick sometimes too late. As I believe “Observe your application carefully before automating. You may find some signal when web page is ready. It may hide in UI or DOM.“.

Using PageFactory :-

It may solve your problem to a good extent as it has a mechanism of relocating web element every time before performing action. If we use PageFactory in above program, we do not need to relocate element explicitly. But it may not solve StaleElementRefrenceException problem always if DOM refreshes by quick ajax calls or some different mechanism as WebDriver can not identify every time when page is really ready to look for an element.

Using Retry Mechanism in loop – Retry what?? Relocate Web element ??OR relocate Web element till action on element is successful?

Relocating web element in a loop till action is performed successfully is the most efficient way. If you are using PageFactory, implementing retry mechanism may be little easier.

Retry mechanism also sucks when you have a list of WebElement of dynamic applications where things change every moment. In this case retry may give you different count of element and you may unable to decide which indexed element you need.

A typical example code is below :-

If you have any doubt, feel free to comment below.
If you like my posts, please like, comment, share and subscribe.

You can find all Selenium related post here.
You can find all API manual and automation related posts here.
You can find frequently asked Java Programs here.

Author: Amod Mahajan

A software Tester who is paid to judge products developed by others. Currently getting paid in American Dollars. Writing technical posts and creating YouTube videos are my hobbies.

9 thoughts on “StaleElementReferenceException – Element’s “Reference” Stales – Get New Reference Of Element

  1. Thank you Amod , this website is really very helpful .Thanks for sharing your knowledge.
    I have a doubt in line number 22 of the retry loop don’t you think we neede a brake after sendkeys() strstatement , or else since retryNeeded element becomes true if exception occurs this condition will cause infinite loop , even after sending element and causes infinite loop.

  2. Hi Amod,

    I recently joined with MakeSeleniumEasy.

    I would like to say thanks to you for all your posts.

    Being Testers We all know these concepts, but the way you explain everything becomes so simple and easy. Your Efforts are remarkable.

Leave a Reply

Please wait...

Subscribe to new posts to become automation expert

Want to be notified when my new post is published? Get my posts in your inbox.

StaleElementReferenceException – Element’s “Reference” Stales – Get New Reference Of Element

by Amod Mahajan time to read: 6 min
%d bloggers like this: