How To Find Broken Links using Selenium

Hello Friends, In this tutorial we will discuss ‘How to Find broken links using Selenium’. Also we will discuss How to find broken images.

Many times we faced this issue while using any website. We can find the all broken links/images on any web application in few minutes using Selenium.

What is broken links?

A link which gives error code – 404 (Not found) after clicking, that link is broken because that link is no longer exists on this website. There are so many reasons to broke any link.

  • Invalid URL
  • Webpage is no longer exists
  • URL structure is changed
  • Due to firewall restriction

A URL which has 2xx HTTP code is valid link and which has 4xx and 4xx HTTP codes are invalid.

How To Find broken links using Selenium

  1. Find all links of the web page.
  2. Iterate each link and check if they are broken. For this Send HTTP request for the link and read response code.
  1. Find all links by below code:
List <WebElement> links = driver.findElements(By.tagName(“a”));
  1. Iterate each links and check if they are broken:
For (int i =0; i<links.size; i++) 
{
WebElement  ele= links.get(i);

// By using href we can get url of the links
String url =  ele.getAttribute(“href”);

//create object of URL class and pass url in this.

URL link = new URL(url);

//Using HttpURLConnection class create a connection

HttpURLConnection httpcon =  (HttpURLConnection)link.openConnection();
httpcon.connect();

//Now using getResponseCode() to get the response code

If(httpcon.getResponseCode()==200){
System.out.println(“URL is valid link”);

}
elseif((httpcon.getResponseCode()==404){
System.out.println(“URL is broken”);

       }
 }

Explanation of Code:

In first steps we have captured all links of webpage using anchor tag name and stored into web elements data type.

In second step we have Iterate each link using for loop then stored value of href attributes.

Now using HttpURLConnection class we will create a connection. Then using getResponseCode method we can get response code of the link.

If it is equal to 200 then link is valid else link is invalid.


How To Find broken Images in Selenium

From an end user point of view a single broken image on a page could be a wrong impression.

Broken image is a link/broken image that does not show any image, clicking upon it.

The user gets 404 error codes after clicking on it.

  • To finding the broken links we have to check attribute value of img
  • Use the <img> tag to collect all images of the web page.
  • For each <img> tag, find attribute value of src.
  1. Find all images by below code:
List <WebElement> img_list = driver.findElements(By.tagName(“img”));

      2. Iterate each images and check if they are broken:

For (int i =0; i<img_list.size; i++) 
{
WebElement  ele= img_list.get(i);

// By using href we can get url of the links
String url =  ele.getAttribute(“src”);

//create object of URL class and pass url in this.

URL link = new URL(url);

//Using HttpURLConnection class create a connection

HttpURLConnection httpcon =  (HttpURLConnection)link.openConnection();
httpcon.connect();

//Now using getResponseCode() to get the response code

If(httpcon.getResponseCode()==200){
System.out.println(“URL is valid image”);

}
elseif((httpcon.getResponseCode()==404){
System.out.println(“URL is broken”);

       }
 }

 

I hope you have enjoyed this tutorial. Happy Learning 🙂

 

Don’t Miss:

How to Handle Alerts using Selenium

Window Handling in Selenium

 

Leave a Comment