Menu Close
    Hire us Request quote Reading list Switch to dark mode

    Finding Broken Links in a Website using Selenium Webdriver

    Selenium Webdriver is the most widely used open source API  to test the functionality of any website. For using selenium webdriver API we have to first install JDK(1.7 or later) , Eclipse and Selenium Java Client Driver . For installation and configuration of eclipse with webdriver, go through the blog

    After configuration, we have to create a class in our Java project to find the broken links in a website. Here we are testing the broken links in the website .

    Code :-

    We have created a class on a project named Webkul under automationFramework package with following code snippet :-

    Here we have used FirefoxDriver class for checking the links in firefox web browser . If we want to check it in chrome browser we have to just use chromedriver in place of geckodriver and use of ChromeDriver class in place of FirefosDriver class.

    Step by step execution of Code:-

    We will now learn how the code is working in finding out the links.

    Firstly we will import all the required packages in the code. Here the required package is :-

    The methods under this package are used to send Java HTTP Request programmatically.

    Now under the main function first of all we will set the path of geckodriver while using FirefoxDriver class. Below is the code for giving path of the driver :-

    Now we will instantiate the FirefoxDriver :-

    For maximizing the firefox window we used following code :-

    After that we will collect all the links of the website and we will store them in a list and then traverse all the items of the list by below code :-

    Now from the list of Urls we will identify and validate each url by following code snippet :-

    After that we are sending http request. HttpURLConnection is the class having methods to send http request and get http response in return. Here we have set request method as “HEAD” because we want only header not the full body of the document. And connect() method is establishing connection with url and sending requests.

    At the end we are using getResponseCode() method so that we can get response code for the request. And on the basis of the response code we are trying to check link status.

    Thus, we can find all links from website and print whether links are valid or broken.

    Thanks for reading this blog .

    . . .

    Comments (4)

    Add Your Comment

  • priya
    Very Good Post! Thank you so much for sharing this good post, it was so good to read and useful to improve my knowledge as updated one, keep blogging.
    Angular JS Training in Electronic City
    • Garima Pathak
      Thanks for your feedback 🙂
  • Sandeep
    resp variable is not declared so its giving error
    • Garima Pathak
      Thanks for your comment. The issue with the variable has been fixed. You can check now.
  • Back to Top
    Hire Us!
    Brief us about your requirements and we'll get back to you.
    Woo! Hooy!
    We have just received your message and our expert will get back to you shortly.
    Send Again