How to resolve, Stale element exception? if element is no longer attached to the DOM?

The problem

The problem you are probably facing is that the method returns the right (and valid!) element, but when you’re trying to access it a second later, it is stale and throws.

This usually arises when:

  1. You click something that loads a new page asynchronously or at least changes it.
  2. You immediatelly (before the page load could finish) search for an element … and you find it!
  3. The page finally unloads and the new one loads up.
  4. You try to access your previously found element, but now it’s stale, even though the new page contains it, too.

The solutions

There are four ways to solve it I know about:

  1. Use proper waits

    Use proper waits after every anticipated page-load when facing asynchronous pages. Insert an explicit wait after the initial click and wait for the new page / new content to load. Only after that you can try to search for the element you want. This should be the first thing you’ll do. It will increase the robustness of your tests greatly.

  2. The way you did it

    I have been using a variant of your method for two years now (together with the technique above in solution 1) and it absolutely works most of the time and fails only on strange WebDriver bugs. Try to access the found element right after it is found (before returning from the method) via a .isDisplayed() method or something. If it throws, you already know how to search again. If it passes, you have one more (false) assurance.

  3. Use a WebElement that re-finds itself when stale

    Write a WebElement decorator that remembers how it was found and re-find it when it’s accessed and throws. This obviously forces you to use custom findElement() methods that would return instances of your decorator (or, better yet, a decorated WebDriver that would return your instances from usual findElement() and findElemens() methods). Do it like this:

    public class NeverStaleWebElement implements WebElement {
        private WebElement element;
        private final WebDriver driver;
        private final By foundBy;
    
        public NeverStaleWebElement(WebElement element, WebDriver driver, By foundBy) {
            this.element = element;
            this.driver = driver;
            this.foundBy = foundBy;
        }
    
        @Override
        public void click() {
            try {
                element.click();
            } catch (StaleElementReferenceException e) {
                // log exception
    
                // assumes implicit wait, use custom findElement() methods for custom behaviour
                element = driver.findElement(foundBy);
    
                // recursion, consider a conditioned loop instead
                click();
            }
        }
    
        // ... similar for other methods, too
    
    }
    

    Note that while I think that the foundBy info should be accessible from the generic WebElements to make this easier, Selenium developers consider it a mistake to try something like this and have chosen not to make this information public. It’s arguably a bad practice to re-find on stale elements, because you’re re-finding elements implicitly without any mechanism for checking whether it’s justified. The re-finding mechanism could potentially find a completely different element and not the same one again. Also, it fails horribly with findElements() when there are many found elements (you either need to disallow re-finding on elements found by findElements(), or remember the how-manyeth your element was from the returned List).

    I think it would be useful sometimes, but it’s true that nobody would ever use options 1 and 2 which are obviously much better solutions for the robustness of your tests. Use them and only after you’re sure you need this, go for it.

  4. Use a task queue (that can rerun past tasks)

    Implement your whole workflow in a new way!

    • Make a central queue of jobs to run. Make this queue remember past jobs.
    • Implement every needed task (“find an element and click it”, “find an element and send keys to it” etc.) via the Command pattern way. When called, add the task to the central queue which will then (either synchronously or asynchronously, doesn’t matter) run it.
    • Annotate every task with @LoadsNewPage, @Reversible etc. as needed.
    • Most of your tasks will handle their exceptions by themselves, they should be stand-alone.
    • When the queue would encounter a stale element exception, it would take the last task from the task history and re-run it to try again.

    This would obviously take a lot of effort and if not thought through very well, could backfire soon. I used a (lot more complex and powerful) variant of this for resuming failed tests after I manually fixed the page they were on. Under some conditions (for example, on a StaleElementException), a fail would not end the test right away, but would wait (before finally time-outing after 15 seconds), popping up an informative window and giving the user an option to manually refresh the page / click the right button / fix the form / whatever. It would then re-run the failed task or even give a possibility to go some steps back in history (e.g. to the last @LoadsNewPage job).


Final nitpicks

All that said, your original solution could use some polishing. You could combine the two methods into one, more general (or at least make them delegate to this one to reduce code repetition):

WebElement getStaleElem(By by, WebDriver driver) {
    try {
        return driver.findElement(by);
    } catch (StaleElementReferenceException e) {
        System.out.println("Attempting to recover from StaleElementReferenceException ...");
        return getStaleElem(by, driver);
    } catch (NoSuchElementException ele) {
        System.out.println("Attempting to recover from NoSuchElementException ...");
        return getStaleElem(by, driver);
    }
}

With Java 7, even a single multicatch block would be sufficient:

WebElement getStaleElem(By by, WebDriver driver) {
    try {
        return driver.findElement(by);
    } catch (StaleElementReferenceException | NoSuchElementException e) {
        System.out.println("Attempting to recover from " + e.getClass().getSimpleName() + "...");
        return getStaleElem(by, driver);
    }
}

This way, you can greatly reduce the amount of code you need to maintain.

Leave a Comment