如何列出并单击网页中找到的所有 link,以及如何检查 link 是否重定向到 404 页面并在 selenium 中抛出错误?

How to list and click on all links found in webpage and how to check if that link is redirecting to 404 page and throw error in 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"));
            }

提前致谢!

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

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();
            }
        }
    }
}