简体   繁体   中英

Getting Exception with code for finding broken links in webpage with Selenium Webdriver

I wrote this code to get the HTTP response of any links and based on that I am printing if the link is valid or not. But every time when I execute the code, it is going to the catch block. Can you tell, where am I making the mistake?

import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
import javax.net.ssl.HttpsURLConnection;
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.firefox.FirefoxDriver;

public class BrokenLinks {

    public static void main(String[] args) {

        // Telling Selenium to find Chrome Driver
        System.setProperty("webdriver.chrome.driver", "C:\\selenium\\chromedriver.exe");

        // Initialize browser
        ChromeDriver driver = new ChromeDriver();

        // Maximize Window
        driver.manage().window().maximize();

        // Launch Google
        driver.get("https://www.google.co.in/");

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

        System.out.println("Total links are " + links.size());

        for (int i = 0; i <= links.size(); i++) {
            try {
                String nextHref = links.get(i).getAttribute("href");
                // System.out.println(nextHref);
                URL url = new URL("nextHref");
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                connection.setRequestMethod("GET");
                connection.connect();
                int code = connection.getResponseCode();
                System.out.println("code: " + code + "Url" + url);
                /*
                 * if (code == 200) { System.out.println("Valid Link:" +
                 * nextHref);} else { System.out.println("INVALID Link:" +
                 * nextHref);}
                 */
            } catch (Exception e) {
                System.out.println("In Exception");
            }
        }
        // Close the browser
        driver.quit();
    }
}

Remove quotes in the following line. It may works.

URL url = new URL("nextHref");

It should be,

URL url = new URL(nextHref);

Change it

URL url = new URL(nextHref); //changed

Try this code

  // Launch Google
    driver.get("https://www.google.co.in/");

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

    System.out.println("Total links are " + links.size());

    for (int i = 0; i <= links.size(); i++) {
        try {
            String nextHref = links.get(i).getAttribute("href");
            // System.out.println(nextHref);
            URL url = new URL(nextHref); //changed
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("GET");
            connection.connect();
            int code = connection.getResponseCode();
            System.out.println("code: " + code + "Url" + url);
            /*
             * if (code == 200) { System.out.println("Valid Link:" +
             * nextHref);} else { System.out.println("INVALID Link:" +
             * nextHref);}
             */
        } catch (Exception e) {
            System.out.println("In Exception");
        }
    }
    // Close the browser
    driver.quit();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM