How to find all broken links on a page using Selenium

When you need to check all the links in your project, you can do it with Postman or any other API testing tool, but there is an easier way. When you use API testing tools, you need to write all link connections one by one, and when links change, you need to edit all tests one by one again.





java- . pdf, , .





1: HTML : <a href="Adress"></a>



, - <a>



. :





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



LINKS_TAG



- "a". .





2: URL-





String urlLink = link.getAttribute(LINKS_ATTRIBUTE);
      
      



LINKS_ATTRIBUTE



- "href"





3: HTTP- HTTP-





HttpConnection URL. Connection Timeout.





URL url = new URL(urlLink);
HttpURLConnection httpURLConnect=(HttpURLConnection)url.openConnection();
httpURLConnect.setConnectTimeout(5000);
httpURLConnect.connect();
      
      



  • : 100-199





  • : 200-299





  • : 300-399





  • : 400-499





  • : 500-599





, , 400, .





import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;


public class FindAllBrokenLinks {
    public final String DRIVER_PATH = "Drivers/chromedriver";
    public final String DRIVER_TYPE = "webdriver.chrome.driver";
    public WebDriver driver;
    public final String BASE_URL = "https://www.bbc.com/";
    public final String LINKS_ATTRIBUTE = "href";
    public final String LINKS_TAG = "a";

    @BeforeTest
    public void beforeTest(){
        ChromeOptions options = new ChromeOptions();
        options.addArguments("--disable-notifications","--ignore-certificate-errors","--disable-extensions");
        System.setProperty(DRIVER_TYPE,DRIVER_PATH);
        driver = new ChromeDriver(options);
        driver.manage().window().maximize();
        driver.get(BASE_URL);
    }

    @Test
    public void FindAllBrokenLinks() throws Exception{
        List<WebElement> allLinks = driver.findElements(By.tagName(LINKS_TAG));
        for(WebElement link:allLinks){
            try {
                String urlLink = link.getAttribute(LINKS_ATTRIBUTE);
                URL url = new URL(urlLink);
                HttpURLConnection httpURLConnect=(HttpURLConnection)url.openConnection();
                httpURLConnect.setConnectTimeout(5000);
                httpURLConnect.connect();
                if(httpURLConnect.getResponseCode()>=400)
                {
                    System.out.println(urlLink+" - "+httpURLConnect.getResponseMessage()+"is a broken link");
                }
                else{
                    System.out.println(urlLink+" - "+httpURLConnect.getResponseMessage());
                }
            }catch (Exception e) {
            }
        }

    }

    @AfterClass
    public void CloseDriver(){
        driver.close();

    }
}
      
      



URL - BBC URL, 1 49 . :) , .





:





https://www.bbc.com/sport — OK





https://www.bbc.com/reel — OK





https://www.bbc.com/worklife — OK





https://www.bbc.com/travel





https://www.bbc.com/future — OK





https://www.bbc.com/culture — OK





https://www.bbc.com/culture/music — OK





http://www.bbc.co.uk/worldserviceradio/





http://www.bbc.co.uk/programmes/p00wf2qw





https://www.bbc.com/news/world-europe-57039362 — OK






"Java QA Automation Engineer". , , .








All Articles