-2

// this is the exception I am getting

Exception in thread "main" java.lang.ClassCastException: class sun.net.www.protocol.mailto.MailToURLConnection cannot be cast to class java.net.HttpURLConnection (sun.net.www.protocol.mailto.MailToURLConnection and java.net.HttpURLConnection are in module java.base of loader 'bootstrap')


// this is what I tried
package brokenLinks;

import java.io.IOException;
import java.net.HttpURLConnection;
//import java.net.MalformedURLException;
import java.net.URL;
import java.time.Duration;
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 io.github.bonigarcia.wdm.WebDriverManager;

public class ValidateBrokenLinksTest {

    public static void main(String[] args) throws IOException {
        //set up the webdriver driver
        WebDriverManager.chromedriver().setup();
        
        //launch chrome driver
        WebDriver driver = new ChromeDriver();
        
        //maximize the window 
        driver.manage().window().maximize();
        
        //load the url
        driver.get("https://testerscafe.in/");
        
        //implicit wait for page to load
        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(30));
        
        //get all the links in the webpage using tagName called "a" and store them inside list collection
        List<WebElement> elements = driver.findElements(By.tagName("a"));
        
        //get the total number of links available 
        int size = elements.size();
        System.out.println("Number of links available in the webpage :"+size);
        
        //iterate from each link and get the attribute value of each link
        for(WebElement links : elements) {
            String link = links.getAttribute("href");
            
            //load all the links to URL class
            URL url = new URL(link);
          
            
            //set up the connection
            HttpURLConnection connection = (HttpURLConnection)(url.openConnection());
            connection.connect();
            
            //validatation
            if(connection.getResponseCode()>=400) {
                System.out.println(link+" ==> is a broken link");
            }
            else {
                System.out.println(link+" ==> is a valid link");
            }
        }
        //close the webdriver
        driver.quit();
    }
}

M S
  • 1
  • 1

1 Answers1

0

Some of the links on that page are email addresses (info@testerscafe.in). For email links url.openConnection() helpfully returns a MailToURLConnection instead of an HttpURLConnection.

You can exclude the email links and get just the http links with xpath.

Instead of

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

use

List<WebElement> elements = driver.findElements(By.xpath("//a[starts-with(@href,'http')]"));

See this post regarding how the starts-with xpath works.