πŸ’‘ If you like this website, please share it with your friends and network! πŸš€
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 + Java
import 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();