Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wait for an image to be fully loaded Selenium WebDriver

I saw that there is a question: Selenium-Webdriver Ruby --> How to wait for images to be fully loaded after click

But it seems that nobody has directly answered that: I need to wait for an image to be fully loaded, not just to present in the DOM, using selenium WebDriver. What should I do?Currently I'm using

  public static void waitUntilVisible(WebDriver webDriver, By locator) {
    WebDriverWait driverWait = new WebDriverWait(webDriver, TIMEOUT);
    driverWait.until(visibilityOfElementLocated(locator));
  }

which uses

  /**
   * An expectation for checking that an element is present on the DOM of a page and visible.
   * Visibility means that the element is not only displayed but also has a height and width that is
   * greater than 0.
   *
   * @param locator used to find the element
   * @return the WebElement once it is located and visible
   */
  public static ExpectedCondition<WebElement> visibilityOfElementLocated(
    final By locator) {
    return new ExpectedCondition<WebElement>() {
      @Override
      public WebElement apply(WebDriver driver) {
        try {
          return elementIfVisible(findElement(locator, driver));
        } catch (StaleElementReferenceException e) {
          return null;
        }
      }

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

but if an image has already an height and width it's probably counted as visible even if the image hasn't loaded

like image 970
Nicola Peluchetti Avatar asked Mar 07 '17 18:03

Nicola Peluchetti


People also ask

Which method will wait till page gets loaded fully?

We can wait until the page is completely loaded in Selenium webdriver by using the JavaScript Executor. Selenium can run JavaScript commands with the help of the executeScript method.

How can I tell if an image is loaded or not in Selenium?

We can check if an image is displayed on page with Selenium. To verify an image we shall take the help of Javascript Executor. We shall utilize executeScript method to execute the Javascript commands in Selenium. Then pass the command return arguments[0].

How do you check if page is completely loaded in Selenium?

We can get Selenium to recognize that a page is loaded. We can set the implicit wait for this purpose. It shall make the driver to wait for a specific amount of time for an element to be available after page loaded.


1 Answers

You could execute some javascript to interrogate the DOM. Specifically, the complete property of an HTMLImageElement.

public static void waitUntilVisible(WebDriver webDriver, By locator) {
    WebDriverWait driverWait = new WebDriverWait(webDriver, TIMEOUT);
    WebElement el = webDriver.findElement(locator);
    driverWait.until(
        new Predicate<WebDriver>() {
            public boolean apply(WebDriver driver) {
                return ((JavascriptExecutor)driver).executeScript("return arguments[0].complete", el);
            }
        }
    );
}

But note that complete:

Returns a Boolean that is true if the browser has finished fetching the image, whether successful or not. It also shows true, if the image has no src value.

like image 97
Mark Lapierre Avatar answered Oct 03 '22 20:10

Mark Lapierre