25. WebDriverWait, ExpectedConditions & Synchronization

The Three Types of Waits in Selenium

1. Implicit Wait: Global timeout for finding elements. Rarely used in modern frameworks because it slows down negative tests. 2. Explicit Wait (WebDriverWait): Targeted condition-based waiting for specific elements (e.g. elementToBeClickable). 3. Fluent Wait: Custom polling frequency and ignored exceptions.
import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.support.ui.ExpectedConditions;
import java.time.Duration;

WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));

// Wait until visible:
WebElement banner = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("banner")));

// Wait until clickable:
WebElement submitBtn = wait.until(ExpectedConditions.elementToBeClickable(By.id("submit-btn")));
submitBtn.click();

Task

  • Implement waitForElementClickable:
  • 1. Accept WebDriver driver, By locator, and int timeoutSeconds.
  • 2. Return new WebDriverWait(driver, Duration.ofSeconds(timeoutSeconds)).until(ExpectedConditions.elementToBeClickable(locator)).
πŸ’‘Common doubts
Did you like the problem?
Lesson 25 of 44
Java
Loading code editor…
Next