How can I find broken or invalid images on a webpage using Webdriver

0 votes
How can I find broken or invalid images on a webpage using Webdriver?
Jul 3, 2019 in Selenium by Lucy
6,442 views

1 answer to this question.

0 votes

Hey @Lucy, to find the broken or invalid images links on a webpage you can use HTTPClient library to check status codes of the images on a page. If they don't load correctly, then it will be registered with likely a 404 but not a 200 status code. We can easily say tell whether the link is broken or not with status codes. If the status code is 404, then image link is invalid/broken. You can try this code snippet to find broken images:

int invalidImageCount = 0; 

WebDriver driver = new FirefoxDriver(); 
driver.get("http://google.com");

List<WebElement> imagesList = driver.findElements(By.tagName("img")); 
System.out.println("Total no. of images are " + imagesList.size()); 

for (WebElement imgElement : imagesList) { 
 if (imgElement != null) { 
  try { 
   HttpClient client = HttpClientBuilder.create().build(); 
   HttpGet request = new HttpGet(imgElement.getAttribute("src")); 
   HttpResponse response = client.execute(request); 

   // verifying response code he HttpStatus should be 200 if not, 
   // increment as invalid images count 

   if (response.getStatusLine().getStatusCode() != 200) 
     invalidImageCount++; 
   } catch (Exception e) { 
     e.printStackTrace(); 
   }
  } 
 } 

System.out.println("Total no. of invalid images are " + invalidImageCount);
answered Jul 3, 2019 by Junaid
Is there any way to get around this error? I only intend to check a certain area with images but not sure how to only target them) :

 org.apache.http.client.ClientProtocolException: URI does not specify a valid host name: data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20width%3D%2224px%22%20height%...
You're probably just missing http:// at the beginning but can you show a bit more of the code?

Related Questions In Selenium

0 votes
2 answers

How can I verify Error Message on a webpage using Selenium Webdriver?

We can verify error messages on a ...READ MORE

answered May 31, 2020 in Selenium by divyang
• 140 points
17,303 views
0 votes
2 answers
0 votes
1 answer

How can I delete all the cookies present on a browser using Webdriver?

Hey jasmine, to delete all the cookies ...READ MORE

answered May 30, 2019 in Selenium by Rajeshwari
1,220 views
0 votes
1 answer

How can I get meta-description of a webpage using Selenium Webdriver?

Hey Sonal, to get the meta-description of ...READ MORE

answered Jul 23, 2019 in Selenium by Abha
• 28,140 points
6,087 views
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,767 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,186 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,261 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,900 views
–1 vote
1 answer
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