Explore Courses Blog Tutorials Interview Questions
0 votes
in DevOps and Agile by (19.7k points)

I'm trying to make Selenium wait for an element that is dynamically added to the DOM after page load. Tried this:


In case it helps, here is fluentWait:

FluentWait fluentWait = new FluentWait<>(webDriver) {

    .withTimeout(30, TimeUnit.SECONDS)

    .pollingEvery(200, TimeUnit.MILLISECONDS);


But it throws a NoSuchElementException - looks like presenceOfElement expects the element to be there so this is flawed. This must be bread and butter to Selenium and don't want to reinvent the wheel... could anyone suggest an alternative, ideally without rolling my own Predicate?

1 Answer

0 votes
by (62.9k points)

You need to call ignoring with exception to ignore while the WebDriver will wait.

FluentWait<WebDriver> fluentWait = new FluentWait<>(driver)

        .withTimeout(30, TimeUnit.SECONDS)

        .pollingEvery(200, TimeUnit.MILLISECONDS)


See the documentation of FluentWait for more info. But beware that this condition is already implemented in ExpectedConditions so you should use

WebElement element = (new WebDriverWait(driver, 10))


*Update for newer versions of Selenium:

withTimeout(long, TimeUnit) has become withTimeout(Duration)

pollingEvery(long, TimeUnit) has become pollingEvery(Duration)

So the code will look as such:

FluentWait<WebDriver> fluentWait = new FluentWait<>(driver)




If you are interested to learn Selenium on a much deeper level and want to become a professional in the testing domain, check out Intellipaat Selenium certification

Browse Categories