SamP
SamP

Reputation: 437

How to wait for a WebElement to be present within the DOM?

Currently I looking for a solution to wait for a specific WebElement to be present within the websites DOM.

Currently I have setup the below method which uses a By locator however I'm looking to use a WebElement instead, any ideas?

As per the JavaDocs:

public static ExpectedCondition visibilityOf(WebElement element): An expectation for checking that an element, known to be present on the DOM of a page, is visible. Visibility means that the element is not only displayed but also has a height and width that is greater than 0.

This existing method above, checks that the element is visible and also present in the DOM but not only present in the DOM.

Current method which uses By instead of WebElement: I can see from reading the selenium documentation you can wait for the presence of an element to be visible within the DOM;

An Example:

public static void waitForElementToAppearInDOM(By by, int timer) {
    try {
        WebDriver driver = getDriver();
        WebDriverWait exists = new WebDriverWait(driver, timer);
        exists.until(ExpectedConditions.presenceOfAllElementsLocatedBy(by));
    } catch(Exception e) {

    }
}

Upvotes: 2

Views: 2081

Answers (6)

computatma
computatma

Reputation: 81

In your code, you should have the 'ID' or 'xpath' as locator. Look at the docs on ExpectedConditions Line 5 would look like:

exists.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By, locator));

Also, look at this presence of element not same as visibility.

Upvotes: -3

undetected Selenium
undetected Selenium

Reputation: 193108

You were pretty correct till public static ExpectedCondition visibilityOf(WebElement element): An expectation for checking that an element, known to be present on the DOM of a page, is visible. Visibility means that the element is not only displayed but also has a height and width that is greater than 0. This existing method above, checks that the element is visible and also present in the DOM but not only present in the DOM.

For simplicity I would rephrase this ExpectedCondition as the expectation to check for the element known to be present within the DOM of a page, is visible.

Of-coarse the two expectations presenceOfElementLocated() and visibilityOf() have a lot of differences between them.


Coming back to your main question, to wait for a specific WebElement to be present using WebElement as an argument is not possible . The reason is simple as the WebElement is yet to be identified in the DOM, which will be identified only after the expectation of presenceOfElementLocated or findElement() succeeds.

This concept is abruptly supported by the list of ExpectedConditions in the JavaDocs related to presence of element(s) which are as follows:


To conclude, you can't pass a WebElement as an argument as an expectation as presenceOfElementLocated() as the WebElement is yet to be identified. But once the element is identified through findElement() or presenceOfElementLocated() this WebElement can be passed an an argument.

To summarize:

  • presenceOfElementLocated(By locator): This expectation is for checking that an element is present in the DOM of a page. This does not necessarily mean that the element is visible.

  • visibilityOf(WebElement element): This expectation is for checking that an element, known to be present on the DOM of a page, is visible. Visibility means that the element is not only displayed but also has a height and width that is greater than 0.

Upvotes: 0

dudycoco
dudycoco

Reputation: 1

Maybe you can have a try like this:

WebDriverWait wait = new WebDriverWait(webDriver, timeoutInSeconds);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id<locator>));

Upvotes: -3

Fenio
Fenio

Reputation: 3625

How to check presence with WebElement as a parameter.

    public boolean isElementPresent(WebElement element) {
        if (element != null) {
            try {
//this line checks the visibility but it's not returned. 
//It's to perform any operation on the WebElement
                element.isDisplayed(); 
                return true;
            } catch (NoSuchElementException e) {
                return false;
            } 
        } else return false;
    }

Upvotes: 0

kessemtini omar
kessemtini omar

Reputation: 21

This method "visibilityOfElementLocated" use By:

Example: //To wait for element visible

WebDriverWait wait = new WebDriverWait(driver, 15);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//input[@id='text3']")));

Upvotes: 0

JeffC
JeffC

Reputation: 25611

A few issues:

  1. Presence means it exists in the DOM but is not necessarily visible, clickable, etc. If you want the element to be visible, then wait for visible.
  2. You are using *Elements* which is plural which will wait for ALL elements found by the locator, not just the one you are specifically looking for. This can lead to confusing failures, etc. if you don't have a unique locator. I would avoid using plural if you only want singular.
  3. You should read the docs carefully. There exists an ExpectedConditions method that does exactly what you are looking for.

public static ExpectedCondition visibilityOf(WebElement element)

Your code should look more like

public static void waitForElementToAppearInDOM(WebElement element, int timer) {
    try {
        new WebDriverWait(getDriver(), timer).until(ExpectedConditions.visibilityOf(element));
    } catch(Exception e) {
        // don't leave an empty catch, do something with it or don't catch it.
    }
}

Upvotes: 2

Related Questions