Explicit Wait pauses the test until a specific condition is true, with a timeout.
Setup:
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(15));
Common ExpectedConditions:
Java
// Wait for element to be visible
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("result")));
// Wait for element to be clickable
WebElement btn = wait.until(ExpectedConditions.elementToBeClickable(By.id("submitBtn")));
btn.click();
// Wait for text to be present
wait.until(ExpectedConditions.textToBePresentInElementLocated(By.id("msg"), "Success"));
// Wait for element to disappear
wait.until(ExpectedConditions.invisibilityOfElementLocated(By.id("loader")));
// Wait for URL to contain a value
wait.until(ExpectedConditions.urlContains("/dashboard"));
// Custom wait with lambda
wait.until(d -> d.findElement(By.id("count")).getText().equals("10"));
Best practice: Always prefer
elementToBeClickablebefore calling.click().
