Skip to content
CodeKruPro

A FluentWait with a custom condition (Java)

QuizMediumJava onlyAsked in: Infosys, TCS, Wipro
Wait<WebDriver> wait = new FluentWait<>(driver)
        .withTimeout(Duration.ofSeconds(30))
        .pollingEvery(Duration.ofMillis(500))
        .ignoring(NoSuchElementException.class);

WebElement status = wait.until(d -> {
    WebElement el = d.findElement(By.id("status"));
    return el.getText().equals("Completed") ? el : null;
});

What does this wait do?

  • Every 500 ms it reads the status; it keeps waiting while the element is missing or the text is not Completed, returns the element when it is, and throws TimeoutException after 30 seconds
  • It fails at once if the element is missing
  • It waits a fixed 30 seconds and then reads the status once
  • It returns null after 30 seconds if the text never changes

This question is part of Pro+

Pro+ opens every question in Interview Prep and the mock interview. Every article and the first 2 questions of each topic stay free. Hands-on Selenium and API tasks follow their course lesson's own lock.

See Pro+

Already have Pro+? Sign in