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?
CodePudding user response:
Rather individually, you need to read the line in it's entire context.
- findElements(): Find all elements within the current page using the given mechanism. This method is affected by the 'implicit wait' times in force at the time of execution. 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.
Here, more than 0 items implies greater then 0 i.e. n > 0
but definitely find all elements within the current page till the timeout is reached.
CodePudding user response:
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))