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

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(); 

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) 
   } catch (Exception e) { 

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,
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?

For this type of message that gets ...READ MORE

answered May 31, 2020 in Selenium by divyang
• 140 points
0 votes
2 answers
0 votes
1 answer
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,060 points
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
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
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
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
–1 vote
1 answer