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
7,923 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,500 points
13,670 views
0 votes
2 answers

Problem while using InternetExplorerDriver in Selenium WebDriver

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

answered Aug 31, 2020 in Selenium by Sri
• 3,190 points
9,141 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,910 points
7,220 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 3, 2020 in Selenium by Sri
• 3,190 points
7,872 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 22, 2020 in Selenium by Abdul
34,232 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP