Find Broken / Invalid Images on a Page

There are cases where we have seen image loading is failed due to many reasons, the most see is "Image not loading - Failed to load the given URL” because of image file is not located in the same location as that is specified or may be image file is corrupted. And it will be very difficult to identify invalid images when there are many in the applications.

To achieve this, we 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 whether the link is broken or not with status codes.

You can see the example as in below image.
Basic authentication in IE using Selenium

You can download Apache HTTPClient from here and add to you build path.

First we will try to find all images on the page by using Webdriver. Below is the syntax:

List<WebElement> imagesList = driver.findElements(By.tagName("img"));

Now iterate through each image and verify response code with HttpStatus and it should be 200 if not, increment invalid images count. We can get the response code using below statement:


Let us look into the example :

package com.linked;

import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClientBuilder;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

public class FindBrokenImages {

	private WebDriver driver;
	private int invalidImageCount;

	public void setUp() {
		driver = new FirefoxDriver();

	public void validateInvalidImages() {
		try {
			invalidImageCount = 0;
			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) {
			System.out.println("Total no. of invalid images are "	+ invalidImageCount);
		} catch (Exception e) {

	public void tearDown() {
		if (driver != null)

	public void verifyimageActive(WebElement imgElement) {
		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) {
Selenium Tutorials: 


tries your above code, but while I try to debug the code, the below line of code do not execute and program ends:

HttpGet request = new HttpGet(imgElement.getAttribute("src"));

Please help.

Add new comment

This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.