java - 检查链接的 URL 状态代码时,无法将 HttpResponseCode 错误解析为类型

标签 java selenium selenium-webdriver webdriver httpurlconnection

我编写了一个简单的程序来检查 URL 状态代码。但是 Eclipse 给我错误说 HttpResponseCode 无法解析为类型。我该怎么办。

import static org.testng.Assert.assertEquals;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
import java.util.concurrent.TimeUnit;

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.interactions.Actions;
import org.testng.Assert;

public class StarWarSocialMenu {

    public static void main(String[] args) throws Exception {       
        String sDriverPath = "C:\\BrowserDriver\\chromedriver.exe";
        System.setProperty("webdriver.chrome.driver", sDriverPath);
        int statusCode;
        final String URL = "https://www.starwars.com/";
        WebDriver driver = new ChromeDriver();
        driver.manage().timeouts().setScriptTimeout(10000, TimeUnit.SECONDS);
        driver.get(URL);
        driver.manage().window().maximize();
        List<WebElement> socialMenu = driver.findElements(By.xpath("//div[@id='nav-external-links']//a")); 

        //System.out.println("Total Amenities "  + socialMenu.size());
        for (WebElement e : socialMenu)
        {
            //System.out.println(e.getAttribute("href"));
            String href = e.getAttribute("href");
            statusCode = new HttpResponseCode().httpResponseCodeViaGet(href);

            if(200 != statusCode) {
                System.out.println(href + " gave a response code of " + statusCode);
            }
        }
    }
}

我该怎么办?如果需要下载 jar 文件,那么我可以从哪里下载。

最佳答案

根据 How to get Response Status Code with Selenium WebDriver在 Selenium WebDriver 中没有直接的方法来检查响应状态代码,因此我们必须使用另一个库。您可以使用Apache HttpClientREST-assured library from Jayway .

httpResponseCodeViaGet()

要使用方法httpResponseCodeViaGet(),您必须下载并使用以下导入:

import io.restassured.RestAssured;

然后你可以使用:

new HttpResponseCode().httpResponseCodeViaGet("http://www.google.com");

解决方案

作为替代方案,您可以使用HttpURLConnectionopenConnection(),并且可以使用以下解决方案:

package demo;

import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class BrokenLinks {

    public static void main(String[] args) throws Exception {

        String sDriverPath = "C:\\Utility\\BrowserDrivers\\chromedriver.exe";
        System.setProperty("webdriver.chrome.driver", sDriverPath);
        String statusCode;
        final String URL = "https://www.starwars.com/";
        WebDriver driver = new ChromeDriver();
        driver.get(URL);
        driver.manage().window().maximize();
        List<WebElement> socialMenu = driver.findElements(By.xpath("//div[@id='nav-external-links']//a")); 
        System.out.println("Total Amenities "  + socialMenu.size());
        for (WebElement e : socialMenu)
        {
            String href = e.getAttribute("href");
            System.out.println(e.getAttribute("href"));
            HttpURLConnection connection = (HttpURLConnection) new URL(href).openConnection();
            connection.connect();
            statusCode = connection.getResponseMessage();
            connection.disconnect();
            if(!statusCode.contains("200")) {
                System.out.println(href + " gave a response code of " + statusCode);
            }
        }
    }
}

关于java - 检查链接的 URL 状态代码时,无法将 HttpResponseCode 错误解析为类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55826090/

相关文章:

Java实时改变多个JFrame的不透明度

java - Spark : Merging 2 columns of a DataSet into a single column

java - 奇数求和不起作用

java - 如何触发对 chrome 扩展按钮的点击?

c# - 如何使用 Selenium Webdriver (C#) 在 Chrome 中处理浏览器身份验证?

Selenium 上的 JavaScriptexecutor setAttribute 值

java - 用 ScrollingImagePanel 替换 JPanel?

python - 为什么这个 python 代码可以在我的 XUbuntu (Ubuntu 20.04) 机器上运行,但不能在我的 Ubuntu 18.04 服务器上运行

python - 如何单击“查看更多”链接并通过 Selenium 和 Python 抓取内容

google-chrome - Python -- 使用 Selenium 打开多个选项卡