<bdo id='STi8I'></bdo><ul id='STi8I'></ul>

      <small id='STi8I'></small><noframes id='STi8I'>

    1. <legend id='STi8I'><style id='STi8I'><dir id='STi8I'><q id='STi8I'></q></dir></style></legend>
    2. <tfoot id='STi8I'></tfoot>

        <i id='STi8I'><tr id='STi8I'><dt id='STi8I'><q id='STi8I'><span id='STi8I'><b id='STi8I'><form id='STi8I'><ins id='STi8I'></ins><ul id='STi8I'></ul><sub id='STi8I'></sub></form><legend id='STi8I'></legend><bdo id='STi8I'><pre id='STi8I'><center id='STi8I'></center></pre></bdo></b><th id='STi8I'></th></span></q></dt></tr></i><div id='STi8I'><tfoot id='STi8I'></tfoot><dl id='STi8I'><fieldset id='STi8I'></fieldset></dl></div>

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

        Error for HttpResponseCode cannot be resolved to a type while checking URL status code for links(检查链接的 URL 状态代码时,无法将 HttpResponseCode 的错误解析为类型)

        <small id='f8i3z'></small><noframes id='f8i3z'>

        • <bdo id='f8i3z'></bdo><ul id='f8i3z'></ul>

            • <legend id='f8i3z'><style id='f8i3z'><dir id='f8i3z'><q id='f8i3z'></q></dir></style></legend>

                  <tfoot id='f8i3z'></tfoot>
                  <i id='f8i3z'><tr id='f8i3z'><dt id='f8i3z'><q id='f8i3z'><span id='f8i3z'><b id='f8i3z'><form id='f8i3z'><ins id='f8i3z'></ins><ul id='f8i3z'></ul><sub id='f8i3z'></sub></form><legend id='f8i3z'></legend><bdo id='f8i3z'><pre id='f8i3z'><center id='f8i3z'></center></pre></bdo></b><th id='f8i3z'></th></span></q></dt></tr></i><div id='f8i3z'><tfoot id='f8i3z'></tfoot><dl id='f8i3z'><fieldset id='f8i3z'></fieldset></dl></div>
                    <tbody id='f8i3z'></tbody>
                  本文介绍了检查链接的 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");
                  

                  解决方案

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

                  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 的错误解析为类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  How to send data to COM PORT using JAVA?(如何使用 JAVA 向 COM PORT 发送数据?)
                  How to make a report page direction to change to quot;rtlquot;?(如何使报表页面方向更改为“rtl?)
                  Use cyrillic .properties file in eclipse project(在 Eclipse 项目中使用西里尔文 .properties 文件)
                  Is there any way to detect an RTL language in Java?(有没有办法在 Java 中检测 RTL 语言?)
                  How to load resource bundle messages from DB in Java?(如何在 Java 中从 DB 加载资源包消息?)
                  How do I change the default locale settings in Java to make them consistent?(如何更改 Java 中的默认语言环境设置以使其保持一致?)
                        <tbody id='SvazW'></tbody>
                      <i id='SvazW'><tr id='SvazW'><dt id='SvazW'><q id='SvazW'><span id='SvazW'><b id='SvazW'><form id='SvazW'><ins id='SvazW'></ins><ul id='SvazW'></ul><sub id='SvazW'></sub></form><legend id='SvazW'></legend><bdo id='SvazW'><pre id='SvazW'><center id='SvazW'></center></pre></bdo></b><th id='SvazW'></th></span></q></dt></tr></i><div id='SvazW'><tfoot id='SvazW'></tfoot><dl id='SvazW'><fieldset id='SvazW'></fieldset></dl></div>
                        <bdo id='SvazW'></bdo><ul id='SvazW'></ul>

                        <small id='SvazW'></small><noframes id='SvazW'>

                        <tfoot id='SvazW'></tfoot>

                          1. <legend id='SvazW'><style id='SvazW'><dir id='SvazW'><q id='SvazW'></q></dir></style></legend>