Search code examples
seleniumselenium-webdriverfindelement

How to use findElements method with implicit waiting?


In the method document it's written:

When implicitly waiting, this method will return as soon as there are more than 0 items in the found collection, or will return an empty list if the timeout is reached

As i see (please fix me if i'm wrong), when the method find one element it returns it without searching the others. So what is the advantage of using this method upon using findElement method?


Solution

  • findElements method returns a list of web elements matching the passed locator while findElement method will always return a single web element.
    Also in many cases you are applying findElement and findElements methods on already fully loaded page. In this case findElements will return you a list of All the web elements matching the passed locator.
    However in order to get all the matching elements in loading page you can't effectively use findElements with implicit wait.
    Expected conditions implementing explicit wait should be used instead.
    In case you know the exact amount of elements matching the passed locator presented on that page you can use this method:

    wait.until(ExpectedConditions.numberOfElementsToBe(element, expectedElementsAmount))
    

    And in case you know that it should be at least some known amount of elements you can use this method:

    wait.until(ExpectedConditions.numberOfElementsToBeMoreThan(element, expectedElementsAmount))