Find Broken Links Using Selenium WebDriver:
One of the key test case is to find broken links on a webpage. Due to existence of broken links, your website reputation gets damaged and there will be a negative impact on your business. It’s mandatory to find and fix all the broken links before release. If a link is not working, we face a message as 404 Page Not Found.
Let’s see some of the HTTP status codes.
Below code fetches all the links of a given website (i.e., SoftwareTestingMaterial.com) using WebDriver commands and reads the status of each href link with the help of HttpURLConnection class.
Click here for more information on HttpURLConnection
Given clear explanation in the comments section with in the program itself. Please go through it to understand the flow.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
package softwareTestingMaterial; 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; public class BrokenLinks { public static void main(String[] args) throws InterruptedException { //Instantiating FirefoxDriver System.setProperty("webdriver.gecko.driver", "D:\\Selenium Environment\\Drivers\\geckodriver.exe"); 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 softwaretestingmaterial.com driver.get("https://www.softwaretestingmaterial.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 static 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()); } } //getResponseCode method returns = IOException - if an error occurred connecting to the server. catch (Exception e) { //e.printStackTrace(); } } } |
If this post on “Finding Broken Links Using Selenium WebDriver” was able to help, then don’t mind sharing it with others.
If you are not regular reader of SoftwareTestingMaterial.com then I highly recommend you to signup for the free email newsletter using the below link.

i want to same with testng please help
Hi Pravash, convert the class in to TestNG class. Try and let me know, if you need any help.