Stale Element Reference Exception Selenium (java)

Stale Element Reference Exception Selenium (java)

StaleElementReferenceException is an exception which tells us that the WebElement that the test is trying to interact with is no longer present in the document. This can happen for a number of reasons but most of the time it is because you have either navigated away from a page with the element contained within or you have closed the dialog that is its parent.

The following example shows two portions of test code:

The first contains logic that will force a stale element exception by clicking a link which will navigate you away from the page before attempting to store the link label's text.

WebElement englishOptionButton = driver.findElement(By.id("js-link-box-en"));
englishOptionButton.click();
String englishText = englishOptionButton.getText();

The second code sample fixes this by clicking the link after the text has been stored.

WebElement englishOptionButton = driver.findElement(By.id("js-link-box-en"));
String englishText = englishOptionButton.getText();
englishOptionButton.click();

Leave a reply

Your email address will not be published. Required fields are marked*

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.