使用Selenium WebDriver在验证页面内的所有链接时获取“连接被拒绝”消息

时间:2017-10-24 07:49:11

标签: selenium selenium-webdriver selenium-chromedriver

我正在尝试验证网页中所有已损坏的链接,但我得到了#34;连接被拒绝:连接"信息。下面是我使用过的代码。我正在使用chrome驱动程序。

public static void verifyLink(String linkUrl) throws IOException {

        URL url = new URL(linkUrl);
        HttpURLConnection httpURLConnect=(HttpURLConnection)url.openConnection();
        httpURLConnect.setConnectTimeout(3000);
        httpURLConnect.connect();

        if(httpURLConnect.getResponseCode()==200) {
            System.out.println(linkUrl+" - "+httpURLConnect.getResponseMessage());
         }

       if(httpURLConnect.getResponseCode()==HttpURLConnection.HTTP_NOT_FOUND) {
            System.out.println(linkUrl+" - "+httpURLConnect.getResponseMessage() + " - "+ HttpURLConnection.HTTP_NOT_FOUND);
         }

    }

请帮帮我。

1 个答案:

答案 0 :(得分:0)

尝试将超时httpURLConnect.setConnectTimeout(3000);从3秒增加到5 或者第二种选择,也许你需要设置一些冷却时间,因为服务器开始认为你正在进行DDoS攻击。
尝试添加超时。例如implicit wait