How to find broken links in Selenium WebDriver
Let’s see how to find broken links in Selenium WebDriver in this post with examples of code in Java. Broken or dead links are links that appear clickable on a webpage, but nothing happens when users click them. There is no action defined for click. The click triggers no action either because the link lacks a defined destination or the destination fails to respond.
What is a broken link and how to find them?
In order to find dead links in Selenium, we use two concepts.
- href attribute in a link (<a> tag) defines the destination. If that is empty or not defined, the tag has nowhere to redirect the user.
- If we can hit an endpoint, it returns 200 OK.
- If it is not reachable, it responds with 400 code.
Now taking forward these known concepts, we will write a program using the class HttpURLConnection.
First, we will find all the links on the webpage using the <a> tag. Next, we will run a for loop over all the <a> tags and find if href attributes are defined for them. If yes, we will see if they respond a 200 OK status code using HttpURLConnection provided method.
Example code to find broken links in Selenium
/**
* This method returns number of broken links
* @param driver- webdriver driver instance
* @param pageUrl- Url to be checked for broken links
* */
public static int findBrokenLinks(WebDriver driver, String pageUrl) {
String url = "";
HttpURLConnection huc = null;
int respCode = 200;
int countBrokenLink = 0;
driver.get(pageUrl);
List < WebElement > links = driver.findElements(By.tagName("a"));
Iterator < WebElement > it = links.iterator();
while (it.hasNext()) {
url = it.next().getAttribute("href");
System.out.println(url);
if (url == null || url.isEmpty()) {
System.out.println("URL is either not configured for anchor tag or it is empty");
continue;
}
try {
huc = (HttpURLConnection)(new URL(url).openConnection());
huc.setRequestMethod("HEAD");
huc.connect();
respCode = huc.getResponseCode();
if (respCode >= 400) {
System.out.println(url + " is a broken link");
countBrokenLink = countBrokenLink + 1;
} else {
System.out.println(url + " is a valid link");
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return countBrokenLink;
}