Tutorials Hut

  • How to Verify All Links Present on a Page

    You can verify all the links present on a page using the “HEAD” or “GET” method in Selenium. Here’s how:

    Verify All links present-

    Get all the links present on the page:

    You can use the findElements method in Selenium to get all the a tags on the page.

    List<WebElement> allLinks = driver.findElements(By.tagName("a"));
    
    

    Loop through each link and verify:

    You can loop through each link and verify using the “HEAD” or “GET” method. Here’s how to verify using the “HEAD” method:

    for (WebElement link : allLinks) {
    	String url = link.getAttribute("href");
    	HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
    	connection.setRequestMethod("HEAD");
    	connection.connect();
    	int responseCode = connection.getResponseCode();
    	if (responseCode >= 400) {
        	System.out.println(url + " is a broken link");
    	} else {
        	System.out.println(url + " is a valid link");
    	}
    }
    
    

    In the above code, we are using the HttpURLConnection class to send a “HEAD” request to the link and getting the response code. If the response code is greater than or equal to 400, it means the link is broken, and if the response code is less than 400, it means the link is valid.

    Here’s how to verify using the “GET” method:

    for (WebElement link : allLinks) {
    	String url = link.getAttribute("href");
    	HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
        connection.setRequestMethod("GET");
    	connection.connect();
    	int responseCode = connection.getResponseCode();
    	if (responseCode >= 400) {
        	System.out.println(url + " is a broken link");
    	} else {
        	System.out.println(url + " is a valid link");
    	}
    }
    
    

    In the above code, we are using the HttpURLConnection class to send a “GET” request to the link and getting the response code. The logic for checking the response code is the same as the “HEAD” method.

    Here is the complete Program:

    package com.tutorialshut.verifyAllLinksOnApage;
     
    import java.io.IOException;
    import java.net.HttpURLConnection;
    import java.net.MalformedURLException;
    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.chrome.ChromeDriver;
    import org.testng.annotations.Test;
     
    import io.github.bonigarcia.wdm.WebDriverManager;
     
    public class VerifyAllLinksOnAPage {
    	private WebDriver driver;
        
    	@Test
    	public void verifyLinksTest() throws MalformedURLException, IOException {
           
        	WebDriverManager.chromedriver().setup();
        	driver = new ChromeDriver();
           
        	driver.get("https://www.example.com");
       
    	List<WebElement> allLinks = driver.findElements(By.tagName("a"));
       
    	for (WebElement link : allLinks) {
        	String url = link.getAttribute("href");
        	HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
        	connection.setRequestMethod("HEAD");
        	connection.connect();
        	int responseCode = connection.getResponseCode();
        	if (responseCode >= 400) {
            	System.out.println(url + " is a broken link");
        	} else {
            	System.out.println(url + " is a valid link");
        	}
    	}
     
    	}  
    	}
    
    

















  • Leave a Reply

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