Error for HttpResponseCode cannot be resolved to a type while checking URL status code for links(检查链接的 URL 状态代码时,无法将 HttpResponseCode 的错误解析为类型)
问题描述
我写了一个简单的程序来检查 URL 状态码.但是 eclipse 给我错误说 HttpResponseCode 不能被解析为一个类型.我该怎么办.
I wrote simple program for checking URL status code. But eclipse giving me error saying HttpResponseCode cannot be resolved to a type. What should I do.
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文件,那么我可以从哪里下载.
What should I do? if jar files need to download then from where I can download.
推荐答案
根据 如何使用 Selenium WebDriver 获取响应状态码 在 Selenium WebDriver 中没有直接的方法来检查响应状态码,所以我们必须使用另一个库.您可以使用 Apache HttpClient 或 Jayway 的 REST-assured 库.
As per How to get Response Status Code with Selenium WebDriver in Selenium WebDriver there is no direct method to check the response status code, so we have to use another library. You can use Apache HttpClient or REST-assured library from Jayway.
要使用 httpResponseCodeViaGet() 方法,您必须下载并使用以下导入:
To use the method httpResponseCodeViaGet() you have to download and use the following import:
import io.restassured.RestAssured;
然后你可以使用:
new HttpResponseCode().httpResponseCodeViaGet("http://www.google.com");
解决方案
作为替代方案,您可以使用 HttpURLConnection 和 openConnection() 并且可以使用以下解决方案:
Solution
As an alternative you can use HttpURLConnection and openConnection() and you can use the following solution:
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);
}
}
}
}
这篇关于检查链接的 URL 状态代码时,无法将 HttpResponseCode 的错误解析为类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:检查链接的 URL 状态代码时,无法将 HttpResponseCode 的错误解析为类型
基础教程推荐
- 使用堆栈算法进行括号/括号匹配 2022-01-01
- Struts2 URL 无法访问 2022-01-01
- Spring AOP错误无法懒惰地为此建议构建thisJoinPoin 2022-09-13
- RabbitMQ:消息保持“未确认"; 2022-01-01
- 存储 20 位数字的数据类型 2022-01-01
- 问题http://apache.org/xml/features/xinclude测试日志4j 2 2022-01-01
- REST Web 服务返回 415 - 不支持的媒体类型 2022-01-01
- 如何对 Java Hashmap 中的值求和 2022-01-01
- 修改 void 函数的输入参数,然后读取 2022-01-01
- 无法复制:“比较方法违反了它的一般约定!" 2022-01-01
