Selenium Waits
Learn how Selenium waits for web elements and conditions before performing automation actions.
What You Will Practice
- Understand why waits are required.
- Practice delayed elements.
- Understand Implicit Wait.
- Understand Explicit Wait.
- Understand Fluent Wait.
- Practice timeout behaviour.
- Understand polling.
- Select the correct wait strategy.
Implicit Wait
The browser waits for an element to become available before throwing a NoSuchElementException.
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
Explicit Wait
Wait for a specific condition before continuing the automation script.
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
wait.until(ExpectedConditions.elementToBeClickable(element));
Fluent Wait
Configure timeout, polling interval and ignored exceptions.
Wait<WebDriver> wait = new FluentWait<>(driver)
.withTimeout(Duration.ofSeconds(15))
.pollingEvery(Duration.ofSeconds(2))
.ignoring(NoSuchElementException.class);
Timeout Handling
Understand what happens when an expected element does not appear.
wait.until(ExpectedConditions.visibilityOfElementLocated(locator));
Choose the Correct Wait
Select the most appropriate wait strategy for each scenario.
Scenario 1
You want Selenium to wait globally for elements throughout the application.
Scenario 2
You need to wait until a specific button becomes clickable.
Scenario 3
You need custom polling and exception handling.
Selenium Waits
driver.manage()
.timeouts()
.implicitlyWait(
Duration.ofSeconds(10)
);
WebDriverWait wait =
new WebDriverWait(
driver,
Duration.ofSeconds(10)
);
wait.until(
ExpectedConditions
.elementToBeClickable(
element
)
);
new FluentWait<WebDriver>(
driver
)
.withTimeout(...)
.pollingEvery(...)
.ignoring(...);