Specified element reference is stale


#1

Hello:
I am getting an intermittent failure in one of my tests that deals with a stale element reference. However, when I run the test again, either in conjunction with other tests or by itself, it will pass. Can you give more information in regards to this error and what it means? Also, why it only happens sometimes? Is there anything we can do to avoid it? It seems to only happen in Edge as well.
Thanks!
Michele


#2

Hello @micheleaz
A stale element reference exception is thrown in one of two cases:

  • The element has been deleted entirely from the webpage.
  • The element is no longer attached to the DOM.

The reason this is happening is that the page that the element was part of has been refreshed, or you have navigated away to another page, a JS library has deleted an element and replaced it with one with the same ID or attributes or an AJAX call to the server refreshed the part of the webpage.
There is more information about this kind of exception on the official selenium page
So, basically, this isn’t a Ranorex Webtestit specific error, but a common selenium one. Now, why is this happening only on the Edge browser, It depends, maybe the execution speed is a bit slower with EdgeDriver, and that behaviour could lead to this exception, but you would have to take a closer look at the step where the exception is being thrown.
A simple solution for this is to use a try/catch block

try {
        yourWebElement.yourMethod();
            
        } catch (StaleElementReferenceException e) {
        yourWebElement =this.wait.until(ExpectedConditions.visibilityOfElementLocated(this.Element));        
        yourWebElement.yourMethod();   
        }

This way you will catch the stale element exception, caused by changed state of the page, and in te catch block, you are again locating the element and executing your desired command.
Hope this will help you.
If you need more info about this, you could hop over to this post.
Regards.


#3

@smatijas

It depends, maybe the execution speed is a bit slower with EndgeDriver

In almost all cases, i stumpled upon some kind of this issue, Edge was too fast :smiley: so it was even harder to diagnose. Now with Edge using Chromium in the future it doesn’t make any difference I guess ^^


#4

@m.grundner
I guess this is one of those Edge cases :smile:


#5

@smatijas :joy: I guess so.


#6

Thanks everybody! I appreciate the help…


#7

I also noticed one thing that is interesting. This error only happens on Edge of course AND elements with link-text locators. When I got away from link-text locators and created instead css or xpath locators for the same element, the errors disappeared.


#8

Nice that the error is gone with the change of locator. I will take a look at the link-text selector created with Ranorex Selocity for possible issues.
Thank you for the updates!
Kind regards


closed #9