首页 > 解决方案 > 如何列出并单击网页中找到的所有链接以及如何检查该链接是否重定向到 404 页面并在 selenium 中引发错误?

问题描述

请检查我尝试过的以下代码在这里我想检查是否所有链接都已打开且不应包含任何 404 页面

public void alllinks() {

        suites.setupEnviroment();
        WebDriver driver = suites.getWebDriver();
        driver.get(suites.WEB_PATH5);
        Dimension d = new Dimension(1455, 900);
        driver.manage().window().setSize(d);

        try {
            List<WebElement> links = driver.findElements(By.tagName("a"));
            ArrayList<String> targets = new ArrayList<String>();
            // collect targets locations
            for (WebElement link : links) {
                targets.add(link.getAttribute("href"));
            }
            for (String target : targets) {
                driver.get(target);
                try {
                    ((WebDriver) links).getPageSource().contains("404");
                } catch (Exception e) {
                    System.out.println("error");
                }

                // do what is needed in the target
            }

            Logger.getLogger("results").log(new LogRecord(Level.INFO,
                    MethodHandles.lookup().lookupClass().getCanonicalName() != null ? "success" : "failure"));
            driver.close();

        } catch (Exception e) {

            Logger.getLogger("results").log(new LogRecord(Level.INFO,
                    MethodHandles.lookup().lookupClass().getCanonicalName() == null ? "success" : "failure"));
            }

提前致谢!

标签: htmleclipseseleniumhttp-status-code-404handle

解决方案


这个样本应该可以完成这项工作。根据您的需要进行调整。

public class FindBrokenLinks {
    private WebDriver driver;
    private int invalidLinks = 0;

    @BeforeClass
    public void setUp() {
        driver = new ChromeDriver();
        driver.get("http://google.com"); // change the url
    }

    @Test
    public void checkForBrokenLinks() {
        try {
            List<WebElement> links = driver.findElements(By.tagName("a"));
            for (WebElement link : links) {
                if (link != null) {
                    checkLink(link);
                }
            }
            System.out.println("Total broken links: " + invalidLinks);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @AfterClass
    public void tearDown() {
        if (driver != null)
            driver.quit();
    }

    public void checkLink(WebElement linkElement) throws IOException {
        HttpURLConnection connection = null;
        try {
            String link = linkElement.getAttribute("href");
            URL url = new URL(link);
            connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("GET");
            connection.connect();

            int responseCode = connection.getResponseCode();

            // change the code for your needs.
            if (responseCode == 404) {
                // you can trow error also ...
                System.out.println("Found invalid link: " + link);
                invalidLinks++;
            }
            connection.disconnect();
        } catch (Exception e) {
            e.printStackTrace();
            if (connection != null) {
                connection.getErrorStream().close();
            }
        }
    }
}

推荐阅读