1

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()?

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
tester
  • 95
  • 1
  • 7

1 Answers1

1

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 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.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Maybe, I did not correctly understand the usage of visibilityOfElementLocated(locator) . My case : I need to login into app and checked that homepage is already opened (for executing further steps). Login process is executing slowly and need to wait 10-15 seconds before homepage is opened. I decided to wait for visibility of one element which is exist on homepage – tester Apr 18 '22 at 13:43
  • Or such approach is not correct? – tester Apr 18 '22 at 13:45
  • @tester Thats the correct approach. – undetected Selenium Apr 18 '22 at 13:50
  • So, maybe I need to increase amount of seconds for waiting? I have tried 300 and I thought that is enough. But it did not – tester Apr 18 '22 at 13:56
  • 300 should have been more then enough. Checkout the answer update. – undetected Selenium Apr 18 '22 at 14:06