How to check if an image is displayed on web page while working with Selenium WebDriver?

0 votes

I'm trying to create a suite of Selenium tests for a web application. I would like to test whether an image is actually displayed which contains valid image data. Can you help me out with this?

May 10, 2019 in Selenium by Vaishnavi
• 1,180 points
3,604 views

1 answer to this question.

0 votes

I've come across a similar situation before, where the image src is as expected but the image is not displayed on the page.

You can check if the image is displayed or not by using the interface, JavaScriptExcecutor.

You can use this code to  Pass the image (WebElement)-

 Object result = ((JavascriptExecutor) driver).executeScript(
   "return arguments[0].complete && "+
   "typeof arguments[0].naturalWidth != \"undefined\" && "+
   "arguments[0].naturalWidth > 0", image);

    boolean loaded = false;
    if (result instanceof Boolean) {
      loaded = (Boolean) result;
      System.out.println(loaded);
    }

You can actually verify if the image is loaded on the webpage. 

Hope this helps:)

answered May 10, 2019 by Surya
• 970 points

Related Questions In Selenium

0 votes
1 answer
0 votes
1 answer
0 votes
2 answers

Finding WebDriver element with Class Name in java

The better way to handle this element ...READ MORE

answered Apr 10, 2018 in Selenium by nsv999
• 5,520 points
3,780 views
0 votes
2 answers

Problem while using InternetExplorerDriver in Selenium WebDriver

enable trusted connection  in internet explorer by ...READ MORE

answered Aug 31 in Selenium by Sri
• 2,970 points
4,762 views
0 votes
1 answer

Geo-location microphone camera pop up

To Allow or Block the notification, access using Selenium and you have to ...READ MORE

answered May 11, 2018 in Selenium by Samarpit
• 5,890 points
2,740 views
0 votes
2 answers

How to use such xpath to find web elements

xpath are two types. 1) Absolute XPath:    /html/b ...READ MORE

answered Sep 2 in Selenium by Sri
• 2,970 points
3,107 views
0 votes
2 answers
0 votes
2 answers

How to save as PDF on Chrome using Selenium

Vaishanvi, Select  ID  selected dropdown or it ...READ MORE

answered Feb 21 in Selenium by Abdul
17,322 views