Webdriver - Wait for Element Using Java

WebDriver - wait for element using Java

This is how I do it in my code.

WebDriverWait wait = new WebDriverWait(webDriver, timeoutInSeconds);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id<locator>));

or

wait.until(ExpectedConditions.elementToBeClickable(By.id<locator>));

to be precise.

See also:

  • org.openqa.selenium.support.ui.ExpectedConditions for similar shortcuts for various wait scenarios.
  • org.openqa.selenium.support.ui.WebDriverWait for its various constructors.

Selenium Wait for anyone of Element to visible

You can use or expected conditions for that

WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.or(
ExpectedConditions.visibilityOfElementLocated(By.id("id1")),
ExpectedConditions.visibilityOfElementLocated(By.id("id2"))
));

Or use cssSelector or ,

wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("#id1, #id2"));

Selenium wait for an element

You are using Thread.sleep which isn't actually waiting for the element, it's just waiting for 3 seconds regardless.

You could do something like this instead.

private void waitForElement(String element) {
WebDriverWait wait = new WebDriverWait(Driver, 10); // Wait for 10 seconds.
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(element)));
WebElement element = driver.findElement(By.xpath(element));

}

There is no need for the try / catch block either unless your expecting something weird to happen. The above code, will wait for the element to appear for 10 seconds. Not sure if you need to use the last line of code or not.

Hope it helps!

How to wait for a WebElement to be present within the DOM?

You were pretty correct till public static ExpectedCondition visibilityOf(WebElement element): An expectation for checking that an element, known to be present on the DOM of a page, is visible. Visibility means that the element is not only displayed but also has a height and width that is greater than 0. This existing method above, checks that the element is visible and also present in the DOM but not only present in the DOM.

For simplicity I would rephrase this ExpectedCondition as the expectation to check for the element known to be present within the DOM of a page, is visible.

Of-coarse the two expectations presenceOfElementLocated() and visibilityOf() have a lot of differences between them.


Coming back to your main question, to wait for a specific WebElement to be present using WebElement as an argument is not possible . The reason is simple as the WebElement is yet to be identified in the DOM, which will be identified only after the expectation of presenceOfElementLocated or findElement() succeeds.

This concept is abruptly supported by the list of ExpectedConditions in the JavaDocs related to presence of element(s) which are as follows:

  • presenceOfAllElementsLocatedBy(By locator)
  • presenceOfElementLocated(By locator)
  • presenceOfNestedElementLocatedBy(By locator, By childLocator)
  • presenceOfNestedElementLocatedBy(WebElement element, By childLocator)
  • presenceOfNestedElementsLocatedBy(By parent, By childLocator)

To conclude, you can't pass a WebElement as an argument as an expectation as presenceOfElementLocated() as the WebElement is yet to be identified. But once the element is identified through findElement() or presenceOfElementLocated() this WebElement can be passed an an argument.

To summarize:

  • presenceOfElementLocated(By locator): This expectation is for checking that an element is present in the DOM of a page. This does not necessarily mean that the element is visible.

  • visibilityOf(WebElement element): This expectation is for checking that an element, known to be present on the DOM of a page, is visible. Visibility means that the element is not only displayed but also has a height and width that is greater than 0.

How to wait until an element is present in Selenium?

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")));

*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)
.withTimeout(Duration.ofSeconds(30)
.pollingEvery(Duration.ofMillis(200)
.ignoring(NoSuchElementException.class);

Basic tutorial for waiting can be found here.



Related Topics



Leave a reply



Submit