12K Views

How To Find Broken Links Using Selenium WebDriver

Updated 23 June 2023

Facebook Linkedin

As we know that for any website Broken links are bad in terms of business function, because it cause you to lose clients and customer, and Due to existence of website Broken links, our Website reputation may damaged, and there will be negative impact in our business.

Suppose we have Multiple sites and all these Sites contains approax 2000 links , then it is very difficult to Test a website with all links, So we use selenium to make it easy to test a website. Although we have many tools in a market, But in Automation Terms selenium is Best tools to find out Broken links of any Website.

Before I explain how we can find Broken links in selenium, Let’s see some of the HTTP status codes:

200 – valid Link

404 – Link Not Found

400 – Bad Request

401 – Unauthorized

500 – Internal error

Steps to Follow…

1.) Navigate to the interested webpage for e.g. www.uvdesk.com

2.) Collect all the links from the webpage. All the links are associated with the Taga‘.

3.) Create a list of type WebElement to store all the Link elements in to it.

4.) Now Create a Connection using URL object( i.e ., link)

5.) Connect using Connect Method.

6.) Use getResponseCode () to get response code.

7.) Through exception if any error occured.

Consider a below Test case to test all links of home page of ” www.uvdesk.com”.

Click here for more information on HTTPURLConnection

and you can see results in Console.

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

. . .

Leave a Comment

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


5 comments

  • Akshay
    I write same code but i havent get 500 error using this scrpit. but when i test my application manually then one page showing 500 error please help
  • Mahendra
    i am getting error while executing above code
    Exception in thread “main” java.lang.IndexOutOfBoundsException: Index: 4, Size: 4
    at java.util.ArrayList.rangeCheck(Unknown Source)
    at java.util.ArrayList.get(Unknown Source)
    at gmail.linksbroken.main(linksbroken.java:27)
    • akshaykrsingh.bd358 (Moderator)
      Hi Mahendra,
      hope you are well

      This blog is a technical blog. So if you face any issues or have concerns related to this blog then please share with us at support@webkul.com so that our concern team can assist you properly.

      Thanks & Regards
      Webkul Tem

  • Aditya Bhatt
    Shivani I must say your code is the simplest and well defined. I loved it and works flawlessly. Keep up the good work.
    • Himani Gupta (Moderator)
      Thanks Aditya,
      For your appreciation.