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 Tag ‘a‘. 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 package package1; import java.net.HttpURLConnection; import java.net.URL; import java.util.List; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; import org.testng.annotations.Test; public class classlinKtest { @Test public void main() throws InterruptedException { //Instantiating FirefoxDriver System.setProperty("webdriver.gecko.driver", "/home/users/shivani.kukreti/Downloads/geckodriver"); WebDriver driver = new FirefoxDriver(); //Maximize the browser driver.manage().window().maximize(); //Implicit wait for 10 seconds driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); //To launch www.uvdesk.com driver.get("www.uvdesk.com"); //Wait for 5 seconds Thread.sleep(5000); //Used tagName method to collect the list of items with tagName "a" //findElements - to find all the elements with in the current page. It returns a list of all webelements or an empty list if nothing matches List<WebElement> links = driver.findElements(By.tagName("a")); //To print the total number of links System.out.println("Total links are "+links.size()); //used for loop to for(int i=0; i<links.size(); i++) { WebElement element = links.get(i); //By using "href" attribute, we could get the url of the requried link String url=element.getAttribute("href"); //calling verifyLink() method here. Passing the parameter as url which we collected in the above link //See the detailed functionality of the verifyLink(url) method below verifyLink(url); } } // The below function verifyLink(String urlLink) verifies any broken links and return the server status. public void verifyLink(String urlLink) { //Sometimes we may face exception "java.net.MalformedURLException". Keep the code in try catch block to continue the broken link analysis try { //Use URL Class - Create object of the URL Class and pass the urlLink as parameter URL link = new URL(urlLink); // Create a connection using URL object (i.e., link) HttpURLConnection httpConn =(HttpURLConnection)link.openConnection(); //Set the timeout for 2 seconds httpConn.setConnectTimeout(2000); //connect using connect method httpConn.connect(); //use getResponseCode() to get the response code. if(httpConn.getResponseCode()== 200) { System.out.println(urlLink+" - "+httpConn.getResponseMessage()); } if(httpConn.getResponseCode()== 404) { System.out.println(urlLink+" - "+httpConn.getResponseMessage()); } if(httpConn.getResponseCode()== 400) { System.out.println(urlLink+" - "+httpConn.getResponseMessage()); } } if(httpConn.getResponseCode()== 500) { System.out.println(urlLink+" - "+httpConn.getResponseMessage()); } } } //getResponseCode method returns = IOException - if an error occurred connecting to the server. catch (Exception e) { //e.printStackTrace(); } } } 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 package package1; import java.net.HttpURLConnection;import java.net.URL;import java.util.List;import java.util.concurrent.TimeUnit; import org.openqa.selenium.By;import org.openqa.selenium.WebDriver;import org.openqa.selenium.WebElement;import org.openqa.selenium.firefox.FirefoxDriver;import org.testng.annotations.Test; public class classlinKtest { @Test public void main() throws InterruptedException { //Instantiating FirefoxDriver System.setProperty("webdriver.gecko.driver", "/home/users/shivani.kukreti/Downloads/geckodriver"); WebDriver driver = new FirefoxDriver(); //Maximize the browser driver.manage().window().maximize(); //Implicit wait for 10 seconds driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); //To launch www.uvdesk.com driver.get("www.uvdesk.com"); //Wait for 5 seconds Thread.sleep(5000); //Used tagName method to collect the list of items with tagName "a" //findElements - to find all the elements with in the current page. It returns a list of all webelements or an empty list if nothing matches List<WebElement> links = driver.findElements(By.tagName("a")); //To print the total number of links System.out.println("Total links are "+links.size()); //used for loop to for(int i=0; i<links.size(); i++) { WebElement element = links.get(i); //By using "href" attribute, we could get the url of the requried link String url=element.getAttribute("href"); //calling verifyLink() method here. Passing the parameter as url which we collected in the above link //See the detailed functionality of the verifyLink(url) method below verifyLink(url); } } // The below function verifyLink(String urlLink) verifies any broken links and return the server status. public void verifyLink(String urlLink) { //Sometimes we may face exception "java.net.MalformedURLException". Keep the code in try catch block to continue the broken link analysis try { //Use URL Class - Create object of the URL Class and pass the urlLink as parameter URL link = new URL(urlLink); // Create a connection using URL object (i.e., link) HttpURLConnection httpConn =(HttpURLConnection)link.openConnection(); //Set the timeout for 2 seconds httpConn.setConnectTimeout(2000); //connect using connect method httpConn.connect(); //use getResponseCode() to get the response code. if(httpConn.getResponseCode()== 200) { System.out.println(urlLink+" - "+httpConn.getResponseMessage()); } if(httpConn.getResponseCode()== 404) { System.out.println(urlLink+" - "+httpConn.getResponseMessage()); } if(httpConn.getResponseCode()== 400) { System.out.println(urlLink+" - "+httpConn.getResponseMessage()); } } if(httpConn.getResponseCode()== 500) { System.out.println(urlLink+" - "+httpConn.getResponseMessage()); } } } //getResponseCode method returns = IOException - if an error occurred connecting to the server. catch (Exception e) { //e.printStackTrace(); } } } 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.