Difference between presenceOfElementLocated() and presenceOfAllElementsLocatedBy() is Selenium

Viewed 272

From the Documentation I see that
presenceOfElementLocated​(By locator) is "An expectation for checking that an element is present on the DOM of a page" while
presenceOfAllElementsLocatedBy​(By locator) is "An expectation for checking that there is at least one element present on a web page"
So what is the actual difference between these 2 methods?

2 Answers

https://github.com/SeleniumHQ/selenium/blob/bf2fc564eeaeecfd980984119bd18d0a00bdb29e/java/client/src/org/openqa/selenium/support/ui/ExpectedConditions.java

Its an interesting question and there is no much difference than that , if you check the source code the first returns an webelement second returns an webelement array/list.

Thats the only thing it does. IT doesn't wait for all elements it returns true as soon as elements.size>0

Update:

if anyone is wondering why presenceofelement work in case of element not found:

if you check webdriver wait code , you can see that there is an ignoreexception for elementnotfound

https://github.com/SeleniumHQ/selenium/blob/0f197cbd4fa9acdd2ac3ddebbe0cc9b4ca26bff8/java/client/src/org/openqa/selenium/support/ui/WebDriverWait.java

so the code waits till element is found and ignores elementnotfound during the poll time

presenceOfElementLocated​()

As per the documentation, presenceOfElementLocated​() is the expectation for checking that an element is present on the DOM of a page and returns the WebElement once it is located. This does not garuntees that the element is visible.

As per the source code:

    /**
     * An expectation for checking that an element is present on the DOM of a page. This does not
     * necessarily mean that the element is visible.
     *
     * @param locator used to find the element
     * @return the WebElement once it is located
     */
    public static ExpectedCondition<WebElement> presenceOfElementLocated(final By locator) {
      return new ExpectedCondition<WebElement>() {
        @Override
        public WebElement apply(WebDriver driver) {
          return driver.findElement(locator);
        }

        @Override
        public String toString() {
          return "presence of element located by: " + locator;
        }
      };
    }

presenceOfAllElementsLocatedBy()

As per the documentation, presenceOfElementLocated​() is the expectation for checking that there is at least one element present on a web page and returns the list of WebElements once they are located.

As per the source code:

    /**
     * An expectation for checking that there is at least one element present on a web page.
     *
     * @param locator used to find the element
     * @return the list of WebElements once they are located
     */
    public static ExpectedCondition<List<WebElement>> presenceOfAllElementsLocatedBy(
      final By locator) {
      return new ExpectedCondition<List<WebElement>>() {
        @Override
        public List<WebElement> apply(WebDriver driver) {
          List<WebElement> elements = driver.findElements(locator);
          return elements.size() > 0 ? elements : null;
        }

        @Override
        public String toString() {
          return "presence of any elements located by " + locator;
        }
      };
    }

This usecase

The major difference between them is, presenceOfElementLocated() would return the WebElement as soon as it is located while presenceOfAllElementsLocatedBy() would keep on collecting the present elements till the TimeOut is reached or the search reaches to the bottom of the DOM Tree and would return the list of WebElements.

Related