How to Verify Broken Links In a Web Page Using Java Selenium Program?

How to Verify Broken Links In a Web Page Using Java Selenium Program?

As we all know that we can test a web application using Selenium WebDriver and if there are any broken links in the web page and if you have missed that then that effect to the business like loose clients and customer as well.

So test each and every link and verify all the links manually is a challenging task. so for all this operation, we can take the help of selenium to verify all the broken links of the website.

Before we try to how to verify the Broken in a web page. So let’s understand some of the HTTP status codes which we are going use in our automation script:

  • 200 – valid Link
  • 404 – Link Not Found
  • 400 – Bad Request
  • 401 – Unauthorized
  • 500 – Internal error

For find out the broken links, you have to go through with the following steps:

  • Find all the links of the web page based on the <a> tag.
  • Send HTTP requests to all the links and read the HTTP response codes.
  • Based on the response code we can find out the valid and broken links based on the response code.
  • Iterate all the links one by one to get all the response code.

Example Program

package com.selenium.basics;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class VerifyBrokenLinks 
{
   public static void main(String[] args) throws InterruptedException 
   {
      WebDriver driver=new FirefoxDriver();
      driver.get("http://www.google.com");
      Thread.sleep(3000);
      List<WebElement> links=driver.findElements(By.tagName("a"));
      System.out.println("Total No Of Links :- "+links.size());
      for(int i=0;i<links.size();i++)
      {
         WebElement el=links.get(i);
         String url=el.getAttribute("href");
         verifylink(url);
      }
   }
   public static void verifylink(String linkurl)
   {
      try
      {
      URL url=new URL(linkurl);
      HttpURLConnection connection= (HttpURLConnection)url.openConnection();
      connection.setConnectTimeout(3000);
      connection.connect();
      if(connection.getResponseCode()==200)
      {
         System.out.println(linkurl+" - "+connection.getResponseMessage());
      }
      if(connection.getResponseCode()==HttpURLConnection.HTTP_NOT_FOUND)
      {
         System.out.println(linkurl+"-"+connection.getResponseMessage());
      }
      }
      catch(Exception e)
      {
         e.printStackTrace();
      }	
   }
}

Here first we are trying to create a connection with the following code

HttpURLConnection httpURLConnect=(HttpURLConnection)url.openConnection();

After that, we have set the timeout because as we know when an open an URL it first hit the server and according to your request it sends the response or response code, so for happening all this process, it requires some time. Hence we are using the setConnectTimeout() method where we mentioned 3000 which means it will wait 3 seconds for sending the time out exception.

httpURLConnect.setConnectTimeout(3000);

After that, we are trying to connect() method that means we are establishing the connection with the URL and the request is sent to the server.

Now we are trying to get the response with the help of by calling the getResponseCode() method

httpURLConnect.getResponseCode()

If the above method returns 200 then we have already mentioned that’s a valid link, and if we receive any other value except 200 that means that’s a broken link. I hope with the explanation you can able to find any broken links present in your application or not. If still, you are facing any issue then you can comment in the comment section and so that we can fix that issue mutually.

Avatar for Softwaretestingo Editorial Board

I love open-source technologies and am very passionate about software development. I like to share my knowledge with others, especially on technology that's why I have given all the examples as simple as possible to understand for beginners. All the code posted on my blog is developed, compiled, and tested in my development environment. If you find any mistakes or bugs, Please drop an email to softwaretestingo.com@gmail.com, or You can join me on Linkedin.

Leave a Comment