WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(15));// 1. Wait for loading spinner to disappear:
wait.until(ExpectedConditions.invisibilityOfElementLocated(By.cssSelector(".loading-spinner")));
// 2. Retry loop for StaleElementReferenceException:
public static void clickWithStaleRetry(WebDriver driver, By locator, int maxRetries) {
int attempts = 0;
while (attempts < maxRetries) {
try {
driver.findElement(locator).click();
return;
} catch (StaleElementReferenceException e) {
attempts++;
}
}
throw new RuntimeException("Element remained stale after " + maxRetries + " attempts: " + locator);
}