84K Views

How To Find Broken Links Using Selenium WebDriver

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*


2 comments

  • 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
      Thanks Aditya,
      For your appreciation.
  • css.php