Handling StaleElementReferenceException failure while implementing Selenium Waits

Modified on Tue, 28 Mar, 2023 at 4:21 PM

Error Name: 

StaleElementReferenceException

Description: 

StaleElementReferenceException is a common error that occurs in Selenium WebDriver while trying to interact with a web element that is no longer attached to the DOM (Document Object Model).


Understanding the error: When a web page changes dynamically, such as when an element is removed or replaced with a new element, the previously located element becomes stale. StaleElementReferenceException is thrown when the driver tries to interact with a stale element.


Causes:


DOM change: The most common cause of StaleElementReferenceException is a change in the DOM of a web page, such as when an element is removed or replaced by another element.


Asynchronous loading: Another cause can be the use of asynchronous loading in a web page. Asynchronous loading can cause elements to load or disappear at different times, which can cause StaleElementReferenceException.


Multiple frames or windows: If there are multiple frames or windows on a web page, and the driver switches to a different frame or window, the previously located element becomes stale.


Checking the line number and file name where the error occurs is essential for debugging the issue.


After finding the error, please check if any of the above causes can be identified.


One of the causes of StaleElementReferenceException is the DOM change, where the previously located element is no longer present on the web page.


Debugs:


Debug-1: 

Implement Explicit Waits: Explicit waits can be used to wait for an element to become clickable or visible before trying to interact with it. This ensures that the element is present in the DOM and ready to be interacted with.


Debug-2: 

Refactor the code: If the above method does not work, it may be necessary to refactor the code to better handle dynamic changes to the page. This can involve finding a different way to locate the element or using more robust methods for waiting and interacting with the element.



Debug-3: 

Use Page Object Model: Implementing the Page Object Model design pattern can help in reducing StaleElementReferenceException. It involves creating separate classes for each web page in the application and defining all the web elements and their methods in the respective classes.

public class LoginPage {

    private WebDriver driver;

    private WebElement usernameField;

    private WebElement passwordField;

    private WebElement loginButton;


    public LoginPage(WebDriver driver) {

        this.driver = driver;

        PageFactory.initElements(driver, this);

    }


    @FindBy(id = "username")

    public void setUsernameField(WebElement usernameField) {

        this.usernameField = usernameField;

    }


    @FindBy(id = "password")

    public void setPasswordField(WebElement passwordField) {

        this.passwordField = passwordField;

    }


    @FindBy(id = "loginButton")

    public void setLoginButton(WebElement loginButton) {

        this.loginButton = loginButton;

    }


    public void enterUsername(String username) {

        usernameField.clear();

        usernameField.sendKeys(username);

    }


    public void enterPassword(String password) {

        passwordField.clear();

        passwordField.sendKeys(password);

    }


    public void clickLoginButton() {

        loginButton.click();

    }

}





Debug-4: 

Use the try-catch block: Try-catch blocks can be used to catch the StaleElementReferenceException and handle it gracefully by retrying the operation or refreshing the page.



It is recommended to use explicit waits instead of implicit waits as they are more reliable and can handle dynamic changes to the page more effectively.

Was this article helpful?

That’s Great!

Thank you for your feedback

Sorry! We couldn't be helpful

Thank you for your feedback

Let us know how can we improve this article!

Select at least one of the reasons
CAPTCHA verification is required.

Feedback sent

We appreciate your effort and will try to fix the article