How to wait until an element is present in Selenium?

0
0

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

fluentWait.until(ExpectedConditions.presenceOfElement(By.id("elementId"));

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?

  • You must to post comments
0
0

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)
.ignoring(NoSuchElementException.class);

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))
.until(ExpectedConditions.elementToBeClickable(By.id("someid")));

Basic tutorial for waiting can be found here.

  • You must to post comments
Showing 1 result
Your Answer
Post as a guest by filling out the fields below or if you already have an account.
Name*
E-mail*
Website