With the widespread of JavaScript we more and more met with one of our favourite exception which is the StaleElementReferenceException.
It can be tricky because it does not have to appear on the test development machine. This issue can hide in the test code and appear in unexpected situations.

The exception happens when the dom changes and the system can’t be sure if the Webelement is still on the page.
Dom changes can be caused by page refresh, javascript effects, ajax responses and so on.

There are 3 quick strategies/solutions we can use in these cases:

  1. Instead using simple wait like this:

    Put the wait into a “while”, catch the exception and continue the waiting. This way the system will always reload the specific element and the dom change will not affect the wait function.

    	int count = 0;
    	while (count < 4) {
    			try {
    				waitHandler(ExpectedConditions.elementToBeClickable(locator),BASIC_WAIT_TIME_IN_SECONDS / 4);
    			} catch (TimeoutException e) {
    				Reporter.log("Timeout: " + e.getMessage());
    			} catch (StaleElementReferenceException e) {
    				Reporter.log("Trying to recover from a stale element :" + e.getMessage());
  2. Get the elements directly before their usage
    Let examine the following lines:

    	WebElement elementField = getMyDriver().findElement(by);
    	WebElement elementButton = getMyDriver().findElement(by);

    In the following code when you click on the field there can be some javascript validation which can change the appearance of the whole button.
    It is much safer to get the Element directly before its usage.

    	WebElement elementField = getMyDriver().findElement(by1);
    	WebElement elementButton = getMyDriver().findElement(by2);
  3. Store the locators instead of using the WebElement references.
    In cases where you have to keep doing a function while an element is visible, you can get StaleElementReferenceException when the element changes.

    	WebElement errorMessage = getMyDriver().findElement(by);	

    In the code above we do doY and wait with waitForY until the error message is not visible. When the condition changes – the error message becomes visible – the loop will be finished.
    The solution for this example is to use the locator in the condition instead of a specific element. That way we will reread the element from the dom and avoid the exception because of the item’s state change.


    The list is incomplete, but the above mentioned strategies can give you ideas and spare some headache.
    One way to further improve test stability is the usage of javascript library with explicit Webdriver waits.
    We will handle some of those cases in the following blog posts.
    Webdriver wait for ajax to finish and jquery animation

Posted By Tihomir Turzai

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>