Explicit wait helps your test wait for a specific condition before continuing. This avoids errors when elements take time to appear or become ready.
0
0
Explicit wait (WebDriverWait) in Selenium Java
Introduction
Waiting for a button to become clickable before clicking it.
Waiting for a text message to appear after submitting a form.
Waiting for a page element to load after navigation.
Waiting for a dropdown list to be populated before selecting an option.
Syntax
Selenium Java
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10)); wait.until(ExpectedConditions.elementToBeClickable(By.id("elementId")));
Use WebDriverWait with a maximum wait time.
Use ExpectedConditions to specify what to wait for.
Examples
Waits up to 5 seconds for an element with class 'my-class' to be visible.
Selenium Java
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(5)); wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector(".my-class")));
Waits up to 15 seconds for the page title to contain the word 'Welcome'.
Selenium Java
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(15)); wait.until(ExpectedConditions.titleContains("Welcome"));
Sample Program
This test opens a login page, waits for the username field and login button to be ready, performs login, then waits for a welcome message to confirm success.
Selenium Java
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.WebDriverWait; import java.time.Duration; public class ExplicitWaitExample { public static void main(String[] args) { System.setProperty("webdriver.chrome.driver", "path/to/chromedriver"); WebDriver driver = new ChromeDriver(); try { driver.get("https://example.com/login"); WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10)); // Wait for username field to be visible WebElement username = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("username"))); username.sendKeys("testuser"); // Wait for login button to be clickable WebElement loginButton = wait.until(ExpectedConditions.elementToBeClickable(By.id("loginBtn"))); loginButton.click(); // Wait for welcome message to appear WebElement welcomeMsg = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("welcomeMessage"))); System.out.println("Welcome message displayed: " + welcomeMsg.isDisplayed()); } finally { driver.quit(); } } }
OutputSuccess
Important Notes
Always set a reasonable timeout to avoid long waits.
Explicit wait is better than fixed sleep because it waits only as long as needed.
Use specific ExpectedConditions for reliable waits.
Summary
Explicit wait pauses test until a condition is met or timeout occurs.
Use WebDriverWait with ExpectedConditions to wait for elements or page states.
This helps tests run smoothly on slow or dynamic web pages.