Interview #92: Selenium: How do you handle StaleElement exception?
Software Testing Studio | WhatsApp 91-9606623245
Looking for Job change? WhatsApp 91-9606623245
StaleElementReferenceException occurs in Selenium WebDriver when an element is no longer attached to the DOM (Document Object Model). This usually happens in dynamic web applications where elements are refreshed, removed, or reloaded due to AJAX calls, page refreshes, or DOM updates.
Disclaimer: For QA-Testing Jobs/Training, WhatsApp us @ 91-9606623245
Common Causes of StaleElementReferenceException
WebElement button = driver.findElement(By.id("submit"));
driver.navigate().refresh(); // Refreshing the page
button.click(); // Causes StaleElementReferenceException
WebElement element = driver.findElement(By.id("dynamicElement"));
// Some AJAX call updates the element
element.click(); // Throws StaleElementReferenceException
How to Handle StaleElementReferenceException?
There are several strategies to handle StaleElementReferenceException effectively:
1. Use Explicit Wait with ExpectedConditions.refreshed()
Selenium provides ExpectedConditions.refreshed() which re-locates the element before performing any action.
Example:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class StaleElementHandling {
public static void main(String[] args) {
WebDriver driver = // Initialize WebDriver
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element = driver.findElement(By.id("dynamicElement"));
element = wait.until(ExpectedConditions.refreshed(ExpectedConditions.elementToBeClickable(element)));
element.click();
}
}
Why it works?
2. Re-locate the Element Before Each Action
Instead of storing the element reference, locate it every time before performing an action.
Example:
for (int i = 0; i < 3; i++) {
try {
WebElement element = driver.findElement(By.id("dynamicElement"));
element.click();
break; // Exit loop if successful
} catch (StaleElementReferenceException e) {
System.out.println("Retrying due to StaleElementReferenceException...");
}
}
Why it works?
3. Wrap Actions in a Retry Mechanism
Implement a retry mechanism to handle stale elements dynamically.
Example:
领英推荐
public void clickWithRetry(WebDriver driver, By locator, int retries) {
for (int i = 0; i < retries; i++) {
try {
driver.findElement(locator).click();
return; // Exit method if successful
} catch (StaleElementReferenceException e) {
System.out.println("Retrying... Attempt: " + (i + 1));
}
}
throw new RuntimeException("Element still stale after " + retries + " retries");
}
Usage:
clickWithRetry(driver, By.id("submitButton"), 3);
Why it works?
4. Introduce a Short Wait Before Retrying
If the element is frequently updated, adding a small Thread.sleep() before retrying can help.
Example:
for (int i = 0; i < 3; i++) {
try {
WebElement element = driver.findElement(By.id("dynamicElement"));
element.click();
break;
} catch (StaleElementReferenceException e) {
try {
Thread.sleep(500); // Small wait before retrying
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
}
Why it works?
5. Use Try-Catch with FluentWait
Using FluentWait, you can specify a timeout, polling frequency, and exception handling.
Example:
import org.openqa.selenium.support.ui.FluentWait;
import java.time.Duration;
import java.util.function.Function;
public void handleStaleElement(WebDriver driver, By locator) {
FluentWait<WebDriver> wait = new FluentWait<>(driver)
.withTimeout(Duration.ofSeconds(10))
.pollingEvery(Duration.ofMillis(500))
.ignoring(StaleElementReferenceException.class);
WebElement element = wait.until(new Function<WebDriver, WebElement>() {
public WebElement apply(WebDriver driver) {
return driver.findElement(locator);
}
});
element.click();
}
Why it works?
6. Check for Element Presence Before Performing Actions
Ensure the element is present and interactable before clicking it.
Example:
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element = wait.until(ExpectedConditions.presenceOfElementLocated(By.id("dynamicElement")));
if (element.isDisplayed() && element.isEnabled()) {
element.click();
}
Why it works?
Best Practices to Avoid StaleElementReferenceException
Conclusion
StaleElementReferenceException occurs when an element is removed, reloaded, or updated in the DOM. To handle this exception, use Explicit Waits, retry mechanisms, re-locate elements dynamically, and implement FluentWait. By following best practices, you can make your Selenium tests more robust and reliable in handling dynamic web pages.