Handling StaleElementReferenceException in Selenium
Handling StaleElementReferenceException in Selenium occurs when an element that was found
earlier in the DOM is no longer valid or present. This often happens if the DOM is refreshed or
updated. Here are five different ways to handle this exception using Selenium WebDriverManager in
Java, along with real-time examples.
1. Retrying to Find the Element
Retrying to find the element after catching the exception ensures that we are working with the most
current element in the DOM.
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class HandleStaleElement {
public static void main(String[] args) {
[Link]().setup();
WebDriver driver = new ChromeDriver();
[Link]("[Link]
By elementLocator = [Link]("dynamicElement");
for (int i = 0; i < 3; i++) {
try {
WebElement element = [Link](elementLocator);
[Link]();
break;
} catch (StaleElementReferenceException e) {
// Retry to find the element
[Link]();
2. Using Fluent Wait
Fluent wait allows you to define the maximum amount of time to wait for a condition, as well as the
frequency with which to check the condition.
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class HandleStaleElement {
public static void main(String[] args) {
[Link]().setup();
WebDriver driver = new ChromeDriver();
[Link]("[Link]
By elementLocator = [Link]("dynamicElement");
Wait<WebDriver> wait = new FluentWait<>(driver)
.withTimeout([Link](30))
.pollingEvery([Link](5))
.ignoring([Link]);
WebElement element =
[Link]([Link](elementLocator));
[Link]();
[Link]();
3. Using Try-Catch with a Loop
Using a loop to continuously try to interact with the element until it succeeds or the maximum
attempts are reached.
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class HandleStaleElement {
public static void main(String[] args) {
[Link]().setup();
WebDriver driver = new ChromeDriver();
[Link]("[Link]
By elementLocator = [Link]("dynamicElement");
int attempts = 0;
while (attempts < 5) {
try {
WebElement element = [Link](elementLocator);
[Link]();
break;
} catch (StaleElementReferenceException e) {
attempts++;
[Link]();
}
4. Refetching the Element
Refetching the element inside the catch block to ensure it references the most current version of the
element in the DOM.
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class HandleStaleElement {
public static void main(String[] args) {
[Link]().setup();
WebDriver driver = new ChromeDriver();
[Link]("[Link]
By elementLocator = [Link]("dynamicElement");
WebElement element = [Link](elementLocator);
try {
[Link]();
} catch (StaleElementReferenceException e) {
element = [Link](elementLocator); // Refetch the element
[Link]();
[Link]();
}
5. Waiting for the Element to be Refreshed
Waiting for a specific condition that indicates the element has been refreshed in the DOM.
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class HandleStaleElement {
public static void main(String[] args) {
[Link]().setup();
WebDriver driver = new ChromeDriver();
[Link]("[Link]
By elementLocator = [Link]("dynamicElement");
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element =
[Link]([Link](elementLocator));
try {
[Link]();
} catch (StaleElementReferenceException e) {
element =
[Link]([Link]([Link](elementL
ocator)));
[Link]();
[Link]();
Real-time Example
Suppose you have a dynamic web page where an element's state can change upon user interaction
or through AJAX calls. For instance, a web page with a 'Load More' button that fetches more items
to the list, and each item has a 'Details' button. Handling StaleElementReferenceException is crucial
as the DOM might change frequently. Using the methods above ensures your script can handle
these dynamic changes effectively.