</>

Technology

Selenium

Difficulty

Intermediate

Interview Question

How to find broken images in a page using Selenium WebDriver?

Find all img tags, get each src attribute, send an HTTP GET request using HttpURLConnection, and check if the response code is 400+ to detect broken images.

Answer

Finding Broken Images Using Selenium WebDriver

Approach:

  1. Find all <a> or <img> elements on the page
  2. Get their href/src URLs
  3. Send HTTP GET request using HttpURLConnection
  4. Check if response code >= 400 (broken) or < 400 (valid)

Complete broken images/links checker:

Java
import java.net.HttpURLConnection;
import java.net.URI;
import java.util.List;

@Test
public void findBrokenImages() throws Exception {
    driver.get("https://example.com");

    // Get all links and images on the page
    List<WebElement> links = driver.findElements(By.tagName("a"));
    List<WebElement> images = driver.findElements(By.tagName("img"));

    System.out.println("Total links: " + links.size());
    System.out.println("Total images: " + images.size());

    // Check all links
    for (WebElement link : links) {
        String url = link.getAttribute("href");

        if (url == null || url.isEmpty()) {
            System.out.println("Link has no href — skipping");
            continue;
        }

        URI uri = new URI(url);
        HttpURLConnection httpConn = (HttpURLConnection) uri.toURL().openConnection();
        httpConn.setConnectTimeout(2000);
        httpConn.connect();

        int responseCode = httpConn.getResponseCode();

        if (responseCode >= 400) {
            System.out.println(url + " — is Broken Link (HTTP " + responseCode + ")");
        } else {
            System.out.println(url + " — is valid Link (HTTP " + responseCode + ")");
        }
        httpConn.disconnect();
    }
}

Checking images specifically:

Java
@Test
public void findBrokenImagesOnly() throws Exception {
    driver.get("https://example.com");

    List<WebElement> images = driver.findElements(By.tagName("img"));
    System.out.println("Total images found: " + images.size());

    for (WebElement img : images) {
        String src = img.getAttribute("src");

        if (src == null || src.isEmpty()) {
            System.out.println("Image has no src attribute");
            continue;
        }

        URI uri = new URI(src);
        HttpURLConnection conn = (HttpURLConnection) uri.toURL().openConnection();
        conn.setConnectTimeout(2000);
        conn.connect();

        if (conn.getResponseCode() >= 400) {
            System.out.println("BROKEN: " + src + " (HTTP " + conn.getResponseCode() + ")");
        }
        conn.disconnect();
    }
}

HTTP response code reference:

Code RangeMeaning
200-299Valid (success)
300-399Redirect (usually valid)
400Bad Request
403Forbidden
404Not Found (broken link)
500+Server Error

Check for 404 specifically:

Java
if (conn.getResponseCode() == 404) {
    brokenImages.add(src);
}

Key approach: Get XPath and use tag name ''a'' or ''img''; get all links/images on the page. Use HttpURLConnection to send method GET; get the response code and verify if it is 404/500 — those are broken.

Follow AutomateQA

Related Topics