Detecting and Handling Broken Links in Selenium


In the world of web automation, ensuring that web applications work flawlessly across all browsers and environments is crucial. One critical aspect of web functionality is verifying that all links and images on a page are working correctly.
Broken links and images can lead to poor user experience and decreased website performance. Selenium WebDriver, a popular tool for automating browser interactions, provides an efficient way to detect and handle these issues.
This article explores how to detect broken links and images using Selenium WebDriver and discuss their importance in automation testing.
Understanding Broken Links
A broken link is a hyperlink that points to a resource that is unavailable or no longer exists. When a user clicks on a broken link, they are usually greeted with an error page like “404 Not Found,” indicating the resource could not be found. In a web application, broken links can have several consequences, such as:
- Decreased website traffic.
- Loss of credibility and trust.
- Negative impact on SEO rankings.
It is essential to identify and address broken links to ensure a smooth user experience.
Exploring HTTP Status Codes Related to Broken Links
When testing for broken links, it is important to understand the HTTP status codes that indicate whether a link is broken. Some of the most common HTTP status codes related to broken links include:
- 404 Not Found: The requested resource could not be found on the server.
- 403 Forbidden: The server understands the request, but it refuses to authorize it.
- 500 Internal Server Error: The server encountered an error and could not fulfill the request.
- 301 Moved Permanently: The resource has been moved to a new location, indicating outdated links.
By checking these status codes, Selenium can help identify which links are broken and which are still working.
The Importance of Checking for Broken Links in Selenium Automation
Automated tests are essential for ensuring that a website’s functionality is continuously tested and maintained. Broken links can lead to significant issues, including:
- Poor user experience: Users may be unable to access critical pages or resources, leading to frustration.
- SEO penalties: Search engines may penalize websites with a high number of broken links, affecting visibility and ranking.
- Reputation damage: A website with broken links may give an impression of being outdated or poorly maintained.
By integrating broken link checks into your Selenium tests, you can proactively ensure that all links on your website are valid and working.
Common Causes of Broken Links
Broken links can occur for various reasons, including:
- Removed or deleted pages: If the linked page no longer exists on the server.
- Incorrect URL paths: Typos or wrong paths in the URL may lead to broken links.
- Server issues: Temporary server failures or configuration errors may cause links to break.
- Redirects: A link that was redirected to another page but not updated properly.
- Domain name changes: If the domain name is changed or expired, previously working links will be broken.
Identifying the root cause of broken links is essential for maintaining a healthy website and preventing future occurrences.
How to Detect Broken Links Using Selenium WebDriver
Selenium WebDriver can be used to automate the process of detecting broken links. Selenium interacts with the web page and checks each link’s HTTP response status. Here’s how we can detect broken links using Selenium WebDriver:
- Extract all links on the page using Selenium.
- Make an HTTP request for each link to check its status code.
- Log or report any links that return an HTTP status code indicating the link is broken (e.g., 404, 403).
Example: Locating Broken Links with Selenium
The following is an example of how you can detect broken links using Selenium WebDriver in Java:
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.support.ui.ExpectedConditions;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
public class BrokenLinksTest {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
WebDriver driver = new ChromeDriver();
driver.get("http://example.com");
// Get all links on the page
List links = driver.findElements(By.tagName("a"));
// Iterate over each link
for (WebElement link : links) {
String url = link.getAttribute("href");
verifyLink(url);
}
driver.quit();
}
public static void verifyLink(String linkUrl) {
try {
HttpURLConnection connection = (HttpURLConnection) new URL(linkUrl).openConnection();
connection.setRequestMethod("HEAD");
connection.connect();
int responseCode = connection.getResponseCode();
if (responseCode >= 400) {
System.out.println("Broken link: " + linkUrl + " Response Code: " + responseCode);
} else {
System.out.println("Working link: " + linkUrl + " Response Code: " + responseCode);
}
} catch (Exception e) {
System.out.println("Invalid link: " + linkUrl);
}
}
}
In this example, we use Selenium to find all anchor (<a>) tags on the page. Then, for each link, we send an HTTP HEAD request to check its response code. If the response code is greater than or equal to 400, the link is considered broken.
What Are Broken Images and How to Spot Them in Selenium?
Broken images occur when an image is either missing or cannot be loaded due to an incorrect path, server issues, or other reasons. A broken image typically results in a placeholder being shown in place of the actual image. Like broken links, broken images can negatively impact the user experience. In Selenium, detecting broken images is similar to detecting broken links. You can verify whether an image is loaded properly by checking its src attribute and the HTTP status code.
Identifying Broken Images in Selenium
To detect broken images in Selenium, follow these steps:
- Find all images on the webpage using Selenium.
- Check the src attribute of each image to ensure the image exists.
- Make an HTTP request to validate the image URL and check if it’s broken.
Example: Detecting Broken Images Using Selenium
The following is an example in Java to detect broken images on a webpage:
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
public class BrokenImagesTest {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
WebDriver driver = new ChromeDriver();
driver.get("http://example.com");
// Get all images on the page
List images = driver.findElements(By.tagName("img"));
// Iterate over each image
for (WebElement image : images) {
String imageUrl = image.getAttribute("src");
verifyImage(imageUrl);
}
driver.quit();
}
public static void verifyImage(String imageUrl) {
try {
HttpURLConnection connection = (HttpURLConnection) new URL(imageUrl).openConnection();
connection.setRequestMethod("HEAD");
connection.connect();
int responseCode = connection.getResponseCode();
if (responseCode >= 400) {
System.out.println("Broken image: " + imageUrl + " Response Code: " + responseCode);
} else {
System.out.println("Working image: " + imageUrl + " Response Code: " + responseCode);
}
} catch (Exception e) {
System.out.println("Invalid image: " + imageUrl);
}
}
}
This code works similarly to the broken link detection. It checks each image on the webpage by sending an HTTP HEAD request to verify its availability.
Conclusion
Ensuring that all links and images on your website are functional is essential for providing a positive user experience and maintaining good SEO performance.
Selenium WebDriver provides a simple yet powerful solution to automate the detection of broken links and images across your website.
By integrating these checks into your Selenium automation suite, you can quickly identify and fix any broken resources before they impact users. To further enhance your testing and run Selenium tests across real devices and browsers, consider using tools like BrowserStack Automate. With its cloud-based Selenium grid, you can execute tests on a wide variety of real browser-device combinations, ensuring that your web application works flawlessly in real-world environments.
This can significantly improve the reliability and effectiveness of your testing process.

Contents
- Understanding Broken Links
- Exploring HTTP Status Codes Related to Broken Links
- The Importance of Checking for Broken Links in Selenium Automation
- Common Causes of Broken Links
- How to Detect Broken Links Using Selenium WebDriver
- Example: Locating Broken Links with Selenium
- What Are Broken Images and How to Spot Them in Selenium?
- Identifying Broken Images in Selenium
- Example: Detecting Broken Images Using Selenium
- Conclusion
Subscribe for latest updates
Share this article
Related posts





