Back to All Scenarios
Scenario 13 of 100
Synchronization
Advanced
Wait Until Element Stops Moving (Stable Animation)
πScenario Overview
Wait Until Element Stops Moving (Stable Animation)
Key Takeaways & Cheat Sheet
- βUnderstand that clicking moving elements triggers ElementClickInterceptedException
- βRetrieve bounding client rect position coordinates sequentially
- βUse custom ExpectedCondition checking if element position is unchanged
- βWait until the difference between consecutive coordinates is zero
Short Direct Answer
When elements animate onto the screen (e.g., sliding modals, accordions, or fade-in overlays), clicking them mid-transition can fail. To handle this, implement a custom wait condition that tracks the element's location (`Point` coordinates) at brief intervals (e.g., every 200ms) and resolves only when consecutive coordinate checks return the exact same location.
β οΈ Senior Warning (Red Flag)
Avoid clicking elements during transitional animations (like sliding panels or fade-ins). The coordinate calculated at the start of the click might change by the time the click is dispatched, causing click misses.
π‘ STAR Deep Dive Explanation & Pro Tip
This custom wait condition is incredibly powerful for complex single-page apps (SPAs) with intensive transition animations.
SeleniumAutomation.java
Selenium 4 + Javaimport org.openqa.selenium.Point;
import org.openqa.selenium.support.ui.ExpectedCondition;
// β
Custom ExpectedCondition to wait for element coordinates to stabilize
public static ExpectedCondition<Boolean> elementToBeStable(final By locator) {
return new ExpectedCondition<Boolean>() {
private Point lastLocation = null;
@Override
public Boolean apply(WebDriver driver) {
try {
WebElement element = driver.findElement(locator);
Point currentLocation = element.getLocation();
if (currentLocation.equals(lastLocation)) {
return true; // Location has stabilized
}
lastLocation = currentLocation;
return false;
} catch (Exception e) {
return false;
}
}
};
}
// Usage:
wait.until(elementToBeStable(By.id("sliding-panel")));
driver.findElement(By.id("sliding-panel")).click();