Reputation: 115
I need to wait until some element will be displayed on page.
I have created method in which I have used wait.until(ExpectedConditions.visibilityOfElementLocated())
:
public void waitForElementPresentBy(By locator) {
try{
wait.until(ExpectedConditions.visibilityOfElementLocated(locator));
}catch (Exception e){
Assert.fail("Element was not become visible");
}
}
wait
object was initialized in next way - I have set 300 seconds:
wait = new WebDriverWait(webDriver, 300);
But this method is not executed correctly. It does not wait until my element will be visible.
When I added Thread.sleep(3000)
, it helped.
But how to waiting for element not adding sleep()
?
Upvotes: 1
Views: 3161
Reputation: 193108
As per the API documentation visibilityOfElementLocated(locator)
is the expectation for checking that an element is present on the DOM of a page and visible. Visibility means that the element is not only displayed but also has a height and width that is greater than 0.
visibility_of_element_located() method is pretty much proven and a robust mechanism to wait for the visibility of a certain element. A bit unclear why you felt ...it does not wait until my element will be visible.... A bit of more details would have helped us to understand the issue in a better way.
However, here visibility is considered with respect to Selenium but not to eyes of the user executing the tests.
Note A: If you are using selenium4 you have to use the following WebDriverWait notation:
wait = new WebDriverWait(webDriver, Duration.ofSeconds(300));
Note B: As you are using WebDriverWait remember to remove Implicit Wait conpletely.
Upvotes: 1