Skip to content
Advertisement

Selenium: How do I check if an element is on a page without use implicitlyWait?

How do I check if an element is on a page without use function implicitlyWait? I may check that element is on page with use implicitlyWait:

    driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);

    if (driver.findElements(By.cssSelector(MY_CSS_SELECTOR)).size() != 0) {
        //...
    }

Is there any analogue of the solution?

Advertisement

Answer

You are already using findElements, Note that findElements will not throw any error even the passed locators is not available in HTMLDOM.

If it finds, it will return a list of web elements.

Now findElements will try to POLL the DOM if it does not find anything immediately, so that’s the reason we use ImplicitWait.

Since you’ve mentioned that you do not wanna deal with ImplicitWait, and wants to grab the list of web elements, you either have to use ExplicitWait

WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.cssSelector("css_selector_here")));

or

wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.cssSelector("css_selector_here")));

But note that using explicit wait, if elements are not found then You will likely get TimeOutException.

ExplicitWait:

Explicit waits are available to Selenium clients for imperative, procedural languages. They allow your code to halt program execution, or freeze the thread, until the condition you pass it resolves. The condition is called with a certain frequency until the timeout of the wait is elapsed. This means that for as long as the condition returns a falsy value, it will keep trying and waiting.

Since explicit waits allow you to wait for a condition to occur, they make a good fit for synchronizing the state between the browser and its DOM, and your WebDriver script.

ImplicitWait:

There is a second type of wait that is distinct from explicit wait called implicit wait. By implicitly waiting, WebDriver polls the DOM for a certain duration when trying to find any element. This can be useful when certain elements on the webpage are not available immediately and need some time to load.

Implicit waiting for elements to appear is disabled by default and will need to be manually enabled on a per-session basis. Mixing explicit waits and implicit waits will cause unintended consequences, namely waits sleeping for the maximum time even if the element is available or condition is true.

Warning: Do not mix implicit and explicit waits. Doing so can cause unpredictable wait times. For example, setting an implicit wait of 10 seconds and an explicit wait of 15 seconds could cause a timeout to occur after 20 seconds.

An implicit wait is to tell WebDriver to poll the DOM for a certain amount of time when trying to find an element or elements if they are not immediately available. The default setting is 0, meaning disabled. Once set, the implicit wait is set for the life of the session.

Official Docs

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement