API Testing:



How To Find Broken Links Using Selenium WebDriver

Find Broken Links Using Selenium WebDriver

Many times we face broken hyperlinks on websites. Today we learn how to find broken links using Selenium. Before moving into these topics, let’s learn the fundamental questions.

What are Broken Links?

A broken link (aka dead link or link rots) is a link on a website that doesn’t work (ie., it doesn’t redirect to the page it is meant to) due to one or more of the following reasons.

  • The destination webpage is no longer available (offline or permanently moved).
  • The destination webpage has been moved without a redirect being added.
  • The URL structure(permalinks) of a webpage is changed.
  • An invalid URL(misspelled, mistyped, etc.) is mentioned in the source webpage.
  • Due to firewall or geolocation restriction.

A URL that has a 2xx HTTP status code is valid and URLs have 4xx and 5xx HTTP status codes are invalid. If you face the 4xx status code that it is due to a client-side error and the status code is 5xx which means it is due to a server response error.

Why check for Broken Links?

Webservers return an error message when a user tries to access a broken link. Users will be directed to an error page when they click on a broken link. This leads to a bad user experience. We have to handle them continuously and remove any existing broken links on our website. We can do this process manually. Most of the websites have hundreds or thousand of links and testing all of them manually is not possible. It requires huge time, resources, and effort. Instead of inspecting manually, we can leverage Selenium WebDriver to test broken links.

Don’t miss: Manual Testing vs Automation Testing – Differences Everyone should know

How to verify the Broken Links and images

Follow the below steps to verify broken links.

  1. All the links are tagged with either link <a> or image <img> on a web page. Collect the links based on tags <a>, <img>
  2. Send HTTP request and read HTTP response code of every link.

This way you can find out whether the link is valid or invalid based on response codes.

Find Broken Links Using Selenium WebDriver

One of the key test cases is to find broken links on a webpage. Due to the existence of broken links, your website reputation gets damaged and there will be a negative impact on your business. It’s mandatory to find and fix all the broken links before release. If a link is not working, we face a message as 404 Page Not Found.

Let’s see some of the HTTP status codes.

200 – Valid Link
404 – Link not found
400 – Bad request
401 – Unauthorized
500 – Internal Error
Consider a test case to test all the links on the home page of “”

Below code fetches all the links of a given website (i.e., using WebDriver commands and reads the status of each href link with the help of HttpURLConnection class.

Click here for more information on HttpURLConnection

Given clear explanation in the comments section within the program itself. Please go through it to understand the flow.

If this post on “Finding Broken Links Using Selenium WebDriver” was able to help, then don’t mind sharing it with others.

If you are not a regular reader of then I highly recommend you to signup for the free email newsletter using the below link.

Sharing is caring.

Share on facebook
Share on twitter
Share on linkedin

Like This Post?

We have a lot more where that came from?

We only send really good stuff occasionally, promise.

Rajkumar SM

2 thoughts on “How To Find Broken Links Using Selenium WebDriver”

Leave a Comment

Your email address will not be published. Required fields are marked *

Share on facebook
Share on google
Share on twitter
Share on linkedin
Share on whatsapp

Recent Posts:

Scroll to Top
API Testing eBook