Selenium: simple links checker

Last updated on:


@Test
    public static void linksCheck() throws Exception {

        driver.get("https://saloote.com/");
        List<WebElement> element = driver.findElements(By.tagName("a"));
        for(int i=0;i<element.size();i++) {
            int statusCode=0;
            try{
                statusCode=getResponseCode(element.get(i).getAttribute("href"));
            }catch(Exception e)
            {
                e.printStackTrace();
            }
            if(statusCode==404) {
                System.out.println("Status code 404:  "+ element.get(i).getAttribute("href"));
            }if (statusCode ==200){
                System.out.println("Status code 200: " +  element.get(i).getAttribute("href"));
            }if(statusCode==302){
                System.out.println("Status code 302: " +  element.get(i).getAttribute("href"));
            }
        }
    }
    public static int getResponseCode(String urlString) throws IOException {
        URL url = new URL(urlString);
        HttpURLConnection connection =  (HttpURLConnection)  url.openConnection();
        connection.setRequestMethod("GET");
        connection.connect();
        return connection.getResponseCode();
    }
View Comments

Leave a Comment

Use markdown or basic HTML and be nice.