Careeroza
Never Mix Implicit and Explicit Waits
Why Mixing Is Bad
The Selenium documentation warns: do not use implicit and explicit waits together. Inside an explicit wait, every findElement also waits implicitly, so the total time becomes unpredictable.
| Setup | Waiting for an element that never appears | Actual time before failure |
|---|---|---|
| explicit 10 s only | polls every 500 ms | about 10 s |
| implicit 10 s + explicit 15 s | each poll itself waits 10 s | about 20 s (can vary) |
implicit 10 s + invisibilityOf check | each findElement waits for the element to exist | slow "is it gone?" checks |
Team Rule
- Set implicit wait to 0 (the default) and use explicit waits for everything.
- Wrap common waits in helper methods so tests stay short.
public class Waits {
private final WebDriverWait wait;
public Waits(WebDriver driver) { this.wait = new WebDriverWait(driver, Duration.ofSeconds(10)); }
public WebElement visible(By by) { return wait.until(ExpectedConditions.visibilityOfElementLocated(by)); }
public WebElement clickable(By by) { return wait.until(ExpectedConditions.elementToBeClickable(by)); }
public void gone(By by) { wait.until(ExpectedConditions.invisibilityOfElementLocated(by)); }
}
waits.clickable(By.id("add-to-cart")).click();
waits.gone(By.cssSelector(".spinner"));