【问题标题】:Getting Exception with code for finding broken links in webpage with Selenium Webdriver使用 Selenium Webdriver 在网页中查找损坏链接的代码出现异常
【发布时间】:2017-09-28 05:43:36
【问题描述】:

我编写了这段代码来获取任何链接的 HTTP 响应,并根据我正在打印的链接是否有效。但是每次我执行代码时,它都会进入 catch 块。你能告诉我,我在哪里犯错了吗?

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

【问题讨论】:

  • 哪里有异常。什么是异常
  • @iamsankalp89 例外是 - 没有协议:nextHref
  • 查看我的答案,它现在正在工作
  • nextHref 是一个变量。去掉new URL("nextHref");中的引号
  • @iamsankalp89 感谢您的帮助。

标签: selenium selenium-webdriver httpresponse httpconnection


【解决方案1】:

改变它

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

试试这个代码

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

【讨论】:

  • 在 nexthef 中删除 "" 并且它正在工作。请检查它,如果发现满意的upvote并接受作为答案
【解决方案2】:

删除以下行中的引号。它可能有效。

URL url = new URL("nextHref");

应该是,

URL url = new URL(nextHref);

【讨论】:

  • 感谢您的帮助。
猜你喜欢
  • 2016-05-02
  • 2018-11-04
  • 1970-01-01
  • 1970-01-01
  • 2022-08-19
  • 1970-01-01
  • 2019-01-24
  • 1970-01-01
  • 2016-09-29
相关资源
最近更新 更多