• Skip to main content
  • Skip to primary sidebar
  • Skip to footer
  • Home
  • Programming Languages
    • Java Tutorials
    • Python Tutorials
    • JavaScript Tutorials
  • Automation Tools and Different Tools
    • Web Automation
      • Selenium with Java
        • Selenium Basic
        • Selenium Advance
        • Selenium Realtime
        • Framework
        • Selenium Interview
        • Selenium Videos
        • Selenium with Docker
      • Selenium with Python
      • WebdriverIO
        • Selenium Webdriver C# Tutorial
      • Cypress
      • Playwright
    • TestNG
    • Cucumber
    • Mobile Automation
      • Appium
    • API Testing
      • Postman
      • Rest Assured
      • SOAPUI
    • testRigor
    • Katalon
    • TestProject
    • Serenity BDD
    • Gradle- Build Tool
    • RPA-UiPath
    • Protractor
    • Windows Automation
  • Automation For Manual Testers
  • Services
  • Online Training
  • Contact us
  • About me
  • Follow us
    • Linkedin
    • Facebook Group
    • Facebook Page
    • Instagram

Automation

Selenium WebDriver tutorial Step by Step

You are here: Home / Advance Selenium / How to Find Broken Links Using Selenium Webdriver

How to Find Broken Links Using Selenium Webdriver

August 30, 2018 by Mukesh Otwani 79 Comments

find broken links using selenium

Hi everyone welcomes back once again to Selenium webdriver tutorial. Today we are going to cover the very basic check that is a must in every application check. We will find broken links using selenium and how we can check what is the status of the same.

 

What is  find broken links using selenium

By the name itself, we can identify that we need to  find broken links using selenium it means we need to check the link which is pointing to wrong URL or invalid URL.

 

find broken links using selenium

 

 

I am sure you must have faced 404 page not found an issue in most of the application which is called broken link.

It does not only link you may also have to verify the images as well that we will see in the next tutorial.

While doing validation you only have to verify status

1- 200- Success- ok

 

Scenario for  find broken links using selenium

Before jumping to the code let’s take one simple example to get the actual concept.

Example1- Suppose we have one application which contains 400 links and we need to verify the link is broken or not.

 

Approach 1- 

Manual Process- Go to each link and verify the link is working or not.

Do you think it is the valid approach? No, it will take a full day to verify and you will not get the same efficiency and interest as well.

 

Approach 2-

Smart work- Write a code which will check all the link and will verify the status as well.

Since we all are smart so we will take Smart work and will see how to find broken links using selenium.

Before I start let me introduce HttpURLConnection class which will help us to verify the status of the response.

Precondition- 

1- Selenium setup  should be completed

 

Refer complete video find  broken links using selenium

 

Program for find broken links using selenium

 

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 VerifyLinks {

	public static void main(String[] args) 
	{
		WebDriver driver=new FirefoxDriver();
		
		driver.manage().window().maximize();
		
		driver.get("http://www.google.co.in/");
		
		List<WebElement> links=driver.findElements(By.tagName("a"));
		
		System.out.println("Total links are "+links.size());
		
		for(int i=0;i<links.size();i++)
		{
			
			WebElement ele= links.get(i);
			
			String url=ele.getAttribute("href");
			
			verifyLinkActive(url);
			
		}
		
	}
	
	public static void verifyLinkActive(String linkUrl)
	{
        try 
        {
           URL url = new URL(linkUrl);
           
           HttpURLConnection httpURLConnect=(HttpURLConnection)url.openConnection();
           
           httpURLConnect.setConnectTimeout(3000);
           
           httpURLConnect.connect();
           
           if(httpURLConnect.getResponseCode()==200)
           {
               System.out.println(linkUrl+" - "+httpURLConnect.getResponseMessage());
            }
          if(httpURLConnect.getResponseCode()==HttpURLConnection.HTTP_NOT_FOUND)  
           {
               System.out.println(linkUrl+" - "+httpURLConnect.getResponseMessage() + " - "+ HttpURLConnection.HTTP_NOT_FOUND);
            }
        } catch (Exception e) {
           
        }
    } 
	
	
	 

}

 

Output

You can see we are getting 49 links and all seems OK.

Congrats 🙂

find broken links using selenium

 

 

I hope you can understand how easy and how important to verify the link and images. You can try the above program and let me know if any issue in above program.

 

Filed Under: Advance Selenium Tagged With: find broken links using selenium webdriver

Reader Interactions

Comments

  1. Ajit Nayak says

    August 18, 2023 at 11:59 AM

    I tried the above code . It is only finding the total links but not iterating to get all the links and the status for each when i ran.
    What might be the issue could you let me know.

    Reply
    • Mukesh Otwani says

      August 18, 2023 at 12:45 PM

      Hi Ajit, can you share the console output.

      Reply
  2. Manjusha says

    August 2, 2021 at 5:44 PM

    how to perform automation when links are in excel file and I want to test broken links

    Reply
    • Mukesh Otwani says

      August 4, 2021 at 5:57 PM

      Hi Manjusha,

      Iterate in excel to read all links and add them into some data structure(like List). Then check for each URL using http library.

      Reply
  3. ahmed nur says

    May 19, 2020 at 9:15 PM

    Do you have this in c#

    Reply
    • Mukesh Otwani says

      May 19, 2020 at 11:00 PM

      Hi Ahmed,

      Not as of now but I’ll post it soon…:)

      Reply
  4. Collin Code says

    October 9, 2019 at 1:36 AM

    Any way you have a demonstration of this done in python?

    Reply
    • Mukesh Otwani says

      October 9, 2019 at 10:25 AM

      Hi Colin,

      Not yet, but I’m planning to post it soon…:)

      Reply
  5. Mathi says

    August 2, 2019 at 3:46 PM

    brother i need to create a app for find a number of broken link in a page using ember . How i do that??

    Reply
    • Mukesh Otwani says

      August 2, 2019 at 5:19 PM

      Hi Mathi,

      Apologies, I never worked on ember…:(

      Reply
  6. Neha says

    July 26, 2019 at 8:05 PM

    Hi,

    I am new into coding.. Why we used try and catch to validate the response code?

    Reply
    • Mukesh Otwani says

      July 27, 2019 at 11:24 AM

      HI Neha,

      Please refer this link https://www.javatpoint.com/try-catch-block

      Reply
  7. Karthik says

    July 24, 2019 at 8:04 PM

    I am getting unknownhostexception when running this program

    Reply
    • Mukesh Otwani says

      July 24, 2019 at 10:13 PM

      Hi Karthik,

      Are you running this code behind some proxy or firewall? Also check whether it is http or htpp secured url.

      Reply
  8. Prakash chandra Khulbey says

    June 24, 2019 at 2:08 PM

    Hello Mukesh
    Nice blog…..

    I have a query on above, can i use the same code while automating Mobile Web page or i need to change somthing in it.

    I have used same it is not executing while getting “href” in below line:
    String url=ele.getAttribute(“href”);

    Kindly help me.

    Reply
    • Mukesh Otwani says

      June 24, 2019 at 9:34 PM

      Hi Prakash,

      On mobile web browser also, you can use same code.

      Reply
  9. Lilia says

    May 31, 2019 at 1:40 PM

    Hello,
    I am having total links are 54
    and only 4 links are printed in the console. Why is that?

    INFO: Detected dialect: OSS
    Total links are 54
    https://www.kaffekapslen.dk/til-nespresso.html – OK
    https://www.kaffekapslen.dk/til-dolce-gusto.html – OK
    https://www.kaffekapslen.dk/til-tassimo.html – OK
    https://www.kaffekapslen.dk/kundeservice-og-kontaktoplysninger – OK
    [main] INFO net.serenitybdd.core.Serenity –

    Reply
    • Mukesh Otwani says

      May 31, 2019 at 1:53 PM

      Hi Lilia,

      Can you please mention what xpath have you used ?

      Reply
  10. Rajnish says

    April 1, 2019 at 9:36 AM

    Hi Mukesh,
    How to verify the other page links give errors 404 in the website. because this method only verify the few links but others are not found.

    Reply
    • Mukesh Otwani says

      April 1, 2019 at 5:00 PM

      Hi Rajnish,

      Using Selenium and href attribute, you can only fetch current webpage links. If you want to check links available on other page then you have to navigate to intended page.

      Reply
  11. Girija says

    December 13, 2018 at 7:50 PM

    Hello Mukesh,
    This method is working when broken image gives 404 error page. But what should we do when a website have managed 404 page to standardised error page ?

    Reply
    • Mukesh Otwani says

      December 14, 2018 at 11:18 PM

      Hi Girija,

      In your case, you may need to use GET/POST request using same HttpURLConnection class followed by verification of response of request.

      Reply
  12. Rohit says

    December 11, 2018 at 12:50 PM

    Hi Mukesh,

    Can you please make detail video of Web Services testing(Soupui and other related concepts) ? this will be very helpful.
    Thanks in advance.

    Reply
    • Mukesh Otwani says

      December 11, 2018 at 8:51 PM

      Hi Rohit,

      I’ll post corresponding videos soon…:)

      Reply
  13. Tauseef says

    November 28, 2018 at 3:08 PM

    Hi Mukesh,

    How can we generate report have these columns s.no , Page name, Link , status ?

    Regards,
    Tauseef

    Reply
    • Mukesh Otwani says

      November 29, 2018 at 9:35 AM

      Hi Tauseef,

      To achieve your objective, you have use looping mechanism so that in every iteration, Serial Number comes. For Page Name depends on your naming convention, Page Link get be acquired using driver.getCurrentUrl() and Status you can return as per execution flow end.

      Reply
  14. Pradeep says

    October 27, 2018 at 6:06 AM

    Hi Mukesh, I am facing issue with appium installation I have downloaded all the tools and jar files and installed but padnet, genymotion, appiumserver are not working in my system and selenium web driver is going fine
    Can you please help about this issues if you have done vedeos for this please share.

    I am following your vedeos and past from 3montgs it’s good and helped soo much…thanks

    Reply
    • Mukesh Otwani says

      October 31, 2018 at 10:11 AM

      Hi Pradeep,

      I’ll post few more videos very soon…:)

      Reply
  15. preethi says

    October 16, 2018 at 12:16 PM

    hi mukesh , pls make a video for jmeter

    Reply
    • Mukesh Otwani says

      October 17, 2018 at 9:39 AM

      Hi Preethi,

      Its already in pipeline. Please stay tuned…:)

      Reply
  16. richa shastri says

    September 30, 2018 at 1:55 PM

    Hi , what is good approach to handle stale element reference exceptions

    Reply
    • Mukesh Otwani says

      September 30, 2018 at 1:57 PM

      Hi Richa,

      Please refer this link http://learn-automation.com/how-to-solve-stale-element-reference-exception-in-selenium-webdriver/

      Reply
  17. Daniel says

    September 24, 2018 at 2:54 PM

    Why is the method verifyLinkActive public? I give methods the smallest scope needed, which would in this case be private. Is it necessary that other classes can use this method too?

    Reply
    • Mukesh Otwani says

      September 24, 2018 at 8:56 PM

      Hi Daniel,

      For the sake of simplicity, I made it as public otherwise when you use it into framework then you can implement proper access specifier for any method.

      Reply
  18. Anshul Rajvanshi says

    September 17, 2018 at 8:10 PM

    Hi Mukesh,
    Few questions.
    1) Opening google via Headless is working fine. But not able to open Secure sites – Any solution to it please?
    2) We are suing Selenium 3.X and ROBOT framework. Got to know htmlunit driver is not a part of the package.
    Do we have to add the PATH of htmlunit driver (downloaded separately) to PATH environments variable?

    Reply
    • Mukesh Otwani says

      September 18, 2018 at 11:56 PM

      Hi Anshul,

      Yes, you need to add htmlUnit driver separately to your project. Please check this link for more info

      Reply
  19. Hash says

    September 13, 2018 at 9:55 PM

    Hi Mukesh, how can I verify a text is highlighted or not using Selenium Webdriver.

    Reply
    • Mukesh Otwani says

      September 14, 2018 at 12:32 PM

      Hi Hash,

      Please use JavaScriptExecutor to verify background/foreground color verification.

      Reply
  20. Partho Dutta says

    September 12, 2018 at 4:49 PM

    Hi Mukesh
    In my application first i have to login then i have to find the broken link for entire application. Could he please suggest me how to do this?

    Reply
    • Mukesh Otwani says

      September 12, 2018 at 8:39 PM

      Hi Partho,

      I can’t see any difficulties in your mentioned scenario. Login window is usual and very generic activity. And once you login, then you can find all broken links. If this is not what you meant then kindly elaborate your requirement.

      Reply
  21. Sri Datta says

    January 24, 2017 at 10:24 AM

    Hi Mukesh,

    Im getting java.net.UnknownHostException this exception while running the code. Is it because of proxy issue? Please help.

    Thanks
    Sri Datta

    Reply
    • Mukesh Otwani says

      January 24, 2017 at 4:51 PM

      Yes Please check proxy setting.

      Reply
  22. Vahe says

    January 22, 2017 at 11:29 PM

    Hi Mukesh,
    The videos and the blog you have are great. I’m new in automation and I’m learning a lot from you. Very informative and very easy to understand. This code is working great, too. I just have a question maybe you can help me with that.
    Is there a way to test links only in the content of website? Can we exclude the header and footer links? Do you think it is possible?
    Thank you again!

    Reply
    • Mukesh Otwani says

      January 23, 2017 at 12:16 PM

      Hi Vahe,

      yes, it is possible to test links available in content of website instead of Header and Footer.

      Reply
  23. Gursimran singh says

    January 22, 2017 at 12:46 AM

    mera urlconnection nahi utha raha can u plz help me???

    Reply
    • Mukesh Otwani says

      January 23, 2017 at 12:01 PM

      Hi Gursimran,

      Could you please explain this?

      Reply
  24. Saurabh says

    January 11, 2017 at 12:52 AM

    Hello Mukesh,

    Good Job Mukesh,
    Mukesh I am getting ‘IndexOutOfBondException’ using this code if my web page having more than 300 links. Can you please suggest me what I can do in this case ?

    Reply
    • Mukesh Otwani says

      January 11, 2017 at 9:44 AM

      Hi Saurabh,

      This is java exception which when you refer an index in array which is not available. Or else you can take any List implementation like ArrayList or LinkedList whose length is dynamic in nature. Hope this should solve your problem.

      Reply
  25. Vimal says

    November 23, 2016 at 10:31 PM

    Mukesh,

    I’m very new to Selenium. i just came across your blogs and articles, it is quite amazing and very useful. So im interested to do the POC of my project in Selenium (Currently we are using QTP). i have many doubts by comparing QTP. Can you help me on this. If i get your email id, so that it will be helpful to share my doubts.

    Reply
    • Mukesh Otwani says

      December 6, 2016 at 2:28 PM

      Hi Vimal,

      Are you done with your POC?

      Reply
  26. Vimal says

    November 23, 2016 at 10:25 PM

    Hi Mukesh,

    when i try to find the broken links in my application, im getting following error.
    java.net.ConnectException: Connection refused: connect. Can you please help me on this.

    Reply
    • Mukesh Otwani says

      December 6, 2016 at 2:26 PM

      Hi Vimal,

      It comes with proxy so try to setup proxy and then run the same program.

      Reply
  27. Vimal says

    November 23, 2016 at 8:31 AM

    Hi Mukesh,

    I have doubt in the collection of link object. When i try to find the list of link from the page where the attribute is “A”. But in the collection of objects, am getting some null value for few anchor tag. how do we skip that.

    Reply
    • Mukesh Otwani says

      November 23, 2016 at 1:22 PM

      Hi Vimal,

      You can apply one more condition if a href is null then skip is null.

      Reply
  28. Aman says

    October 12, 2016 at 8:42 PM

    Hi Mukesh,
    very nice explanation
    Q- Can the same code be used to find response code 404?

    Reply
    • Aman says

      October 12, 2016 at 8:46 PM

      Also , there is a separate lib for HttpResponse in Apache, Can it be used. Which is more easier Apache lib or yours method

      Reply
      • Mukesh Otwani says

        October 13, 2016 at 8:50 AM

        Both are same I downloaded separately but if you see it also comes with Selenium bundle.

        Reply
    • Mukesh Otwani says

      October 13, 2016 at 8:49 AM

      yes we can do that 🙂

      Reply
  29. bhaskar says

    August 20, 2016 at 12:28 PM

    kya bath kya bath

    sir,
    How to use Assertion After finding Broken Links and Images

    Reply
    • Mukesh Otwani says

      August 27, 2016 at 11:38 PM

      Hey Bhaskar,

      Check below video for assertion https://www.youtube.com/watch?v=dvUPSnGQZCM&feature=autoshare

      Reply
  30. shreyas says

    July 17, 2016 at 8:27 PM

    Hi Mukesh,

    public static void verifyLinkActive(String linkUrl)
    {
    try
    {
    URL url = new URL(linkUrl);

    What does linkURL does

    Reply
    • Mukesh Otwani says

      July 20, 2016 at 2:06 PM

      Hi Shreyas,

      When you access any url using network URL class will be used.

      Reply
  31. Naresh says

    July 12, 2016 at 11:31 PM

    Hi mukesh,thanks for this post,it is working fine for me.and keep posting this type of videos.

    Reply
    • Mukesh Otwani says

      July 14, 2016 at 11:13 AM

      Hey Naresh,

      Cheers 🙂

      Reply
  32. suhana says

    June 11, 2016 at 12:01 PM

    How to find broken images and links on a site on all pages.

    Reply
    • Mukesh Otwani says

      June 14, 2016 at 9:17 PM

      Hi Suhana,

      Kindly use src instead of href and use img tag. Rest piece of code will remain same.

      Reply
  33. Shantosh says

    June 7, 2016 at 1:05 PM

    Hi Mukesh,

    Well explained. Can you please share the tutorial on how to find the broken Images on the web page.

    Much thanks in advance

    Reply
    • Mukesh Otwani says

      June 9, 2016 at 1:58 AM

      Hi Shantosh,

      Thanks same code will work only make changes in getAttribute. Try src attribute.

      Reply
  34. Jayshreekant says

    April 25, 2016 at 2:14 PM

    It is checking the VeryFirst Link, After that is not checking the other links !

    Reply
    • Mukesh Otwani says

      April 26, 2016 at 5:27 PM

      set proxy and run again.

      Reply
  35. Harshal says

    April 24, 2016 at 3:58 PM

    hi mukesh,
    when i am running your code then i got 48 link count.
    Actual link present in consol-44
    If i run using Testng by removing main method and set priority to both method then i got error.
    org.testng.TestNGException:
    Method verifyLinkActive requires 1 parameters but 0 were supplied in the @Test annotation.

    Reply
    • Mukesh Otwani says

      April 26, 2016 at 5:33 PM

      Hi Harshal,

      please add parameter in method as well.

      Reply
  36. Neha says

    April 22, 2016 at 6:48 AM

    Well explained ..always been a fan of your tutorials.. Gr8 job

    Reply
    • Mukesh Otwani says

      April 26, 2016 at 5:42 PM

      Thanks 🙂 Neha

      Reply
  37. c. says

    April 12, 2016 at 7:50 AM

    I get this exception when I your code at Eclipse
    Exception in thread “main” java.lang.NoClassDefFoundError: com/google/common/base/Function
    at verifyBrokenLinks.main(verifyBrokenLinks.java:15)
    Caused by: java.lang.ClassNotFoundException: com.google.common.base.Function
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    … 1 more

    Reply
    • Mukesh Otwani says

      April 13, 2016 at 11:15 PM

      Hi Seleniu jars are not added completly.

      Reply
  38. Abhishek Gupta says

    April 7, 2016 at 5:02 PM

    Hi Mukesh,

    Can you explain why there is discrepancies in total no. of links. It always comes out to be more in no.
    refer Image : http://s15.postimg.org/hxsuih10b/screenshot_domain_date_time.png
    Also I believe in this depth level is limited to visited Page.

    Reply
    • Mukesh Otwani says

      April 10, 2016 at 12:19 PM

      Hi Abhishek,

      it might due to a tag.

      Reply
  39. Nitin says

    April 5, 2016 at 11:38 AM

    Hi Mukesh,

    Thank you for listing this amazing stuff on your blog. I really love the way you explore things that are so genuine and are in for everyday use. Thank you once again!

    Reply

Leave a Reply Cancel reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Free Selenium Videos

https://www.youtube.com/watch?v=w_iPCT1ETO4

Search topic

Top Posts & Pages

  • Selenium Webdriver tutorial for beginners
  • How To Fix Eclipse Autocomplete Or Code Suggestion In Eclipse
  • Selenium Webdriver C# Tutorial
  • WHAT ARE YOUR EXPECTATIONS FROM US?

Stay connected via Facebook

Stay connected via Facebook

Archives

Footer

Categories

Recent Post

  • Syllabus For Playwright Online Training Program
  • API Testing Using Postman And RestAssured
  • Disable Personalise Your Web Experience Microsoft Edge Prompt In Selenium
  • How To Fix Error: No tests found In Playwright
  • How To Fix Eclipse Autocomplete Or Code Suggestion In Eclipse

Top Posts & Pages

  • Selenium Webdriver tutorial for beginners
  • How To Fix Eclipse Autocomplete Or Code Suggestion In Eclipse
  • Selenium Webdriver C# Tutorial
  • WHAT ARE YOUR EXPECTATIONS FROM US?