Selenium + Java · Lesson 25 of 44

0/44 done

Module 25: Waits

25. WebDriverWait, ExpectedConditions & Synchronization

18 minlesson

Your task

Implement waitForElementClickable:

  1. Accept WebDriver driver, By locator, and int timeoutSeconds.
  2. Return new WebDriverWait(driver, Duration.ofSeconds(timeoutSeconds)).until(ExpectedConditions.elementToBeClickable(locator)).

Write it in the editor, then press Check answer (Ctrl + Enter).

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.
java
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();

Key Takeaways & Walkthrough Summary

Welcome to Phase 4! Synchronization is the single most critical factor for reliable Selenium tests. In this lesson, we break down why Thread.sleep is forbidden, how Implicit wait causes hidden delays, and how to master WebDriverWait with ExpectedConditions. Let's master waits!

Part of the Selenium + Java course (Selenium with Java). Practice this concept live in the Selenium Online Compiler or explore our free automation practice form.

Common Doubts & Lesson Q&A

Can I mix Implicit and Explicit waits?

Never mix them! The Selenium official documentation explicitly warns that mixing implicit and explicit waits can result in unpredictable timeout spikes.

Ready? Solve the task in the editor

Use Check answer to mark it complete. Hints are there if you need them.

Java
Skip