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 in Selenium by Vaishnavi
• 1,080 points
79 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 by Surya
• 950 points

Related Questions In Selenium

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

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,050 points
261 views
0 votes
1 answer
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,130 points
629 views
0 votes
1 answer

How to use such xpath to find web elements

It's much simpler to use: #MyDiv123 > div.super or ...READ MORE

answered Jun 14, 2018 in Selenium by Samarpit
• 5,130 points
257 views
0 votes
1 answer

How to save as PDF on Chrome using Selenium

Hey, Vaishnavi! You could try to disable ...READ MORE

answered Mar 8 in Selenium by Surya
• 950 points
1,777 views