Selenium with Java · Locators and Actions

Waits in Selenium: Implicit, Explicit and Fluent

Lesson 6 of 9 · 9 min read

Web pages load in pieces. If your test clicks a button before it exists, Selenium throws NoSuchElementException. Most "flaky" tests are really timing problems, and waits fix them.

Do not use Thread.sleep

Thread.sleep(5000); // bad

A fixed sleep is either too short (the test fails on a slow day) or too long (every run wastes time). Use a wait that continues as soon as the condition is true.

Implicit wait

driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(5));

Applies to every findElement: Selenium keeps looking for up to 5 seconds. It only waits for an element to exist, not to be visible or clickable. Do not mix it with explicit waits, because the timeouts add up in confusing ways.

Explicit wait (use this)

WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));

WebElement button = wait.until(ExpectedConditions.elementToBeClickable(By.id("submit")));
button.click();

wait.until(ExpectedConditions.urlContains("/practice/account/"));
wait.until(ExpectedConditions.textToBePresentInElementLocated(
        By.cssSelector("[data-testid='account-name']"), "Demo Tester"));

The conditions you will use most

ConditionUse it when
visibilityOfElementLocatedYou want to read or check an element.
elementToBeClickableYou are about to click.
invisibilityOfElementLocatedA spinner or loader must disappear first.
textToBePresentInElementLocatedText changes after an action, such as a status message.
urlContains / titleIsYou expect a new page.
alertIsPresentA JavaScript alert should appear.
numberOfElementsToBeA list or table should have a certain number of rows.

Fluent wait

An explicit wait where you also choose how often to check and which errors to ignore:

Wait<WebDriver> wait = new FluentWait<>(driver)
        .withTimeout(Duration.ofSeconds(15))
        .pollingEvery(Duration.ofMillis(500))
        .ignoring(NoSuchElementException.class);

WebElement result = wait.until(d -> d.findElement(By.id("result")));

Put waits inside helper methods

Instead of writing a wait before every click, wrap it once:

protected void click(By locator) {
    wait.until(ExpectedConditions.elementToBeClickable(locator)).click();
}

protected void type(By locator, String text) {
    WebElement el = wait.until(ExpectedConditions.visibilityOfElementLocated(locator));
    el.clear();
    el.sendKeys(text);
}

You will put these helpers in a BasePage in lesson 8.

Practice: on the practice login page, fail the login 3 times. Wait until the button is disabled, then wait about 30 seconds until it is enabled again, without using Thread.sleep.
All 9 lessons in Selenium with Java
  1. What Is Selenium WebDriver?
  2. Set Up Java, Maven and Selenium
  3. Write Your First Selenium Test
  4. Selenium Locators: ID, CSS, XPath and More
  5. Clicks, Typing, Dropdowns, Alerts, Frames and Windows
  6. Waits in Selenium: Implicit, Explicit and Fluent
  7. TestNG for Selenium: Annotations, Assertions and Data
  8. Page Object Model in Selenium Java
  9. Selenium Framework Structure, Reports and CI