Finding Broken Images Using Selenium WebDriver
Approach:
- ✓Find all
<a>or<img>elements on the page - ✓Get their
href/srcURLs - ✓Send HTTP GET request using
HttpURLConnection - ✓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 Range | Meaning |
|---|---|
| 200-299 | Valid (success) |
| 300-399 | Redirect (usually valid) |
| 400 | Bad Request |
| 403 | Forbidden |
| 404 | Not 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.
