我写了这段代码来获取任何链接的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();
}
}
答案 0 :(得分:1)
删除以下行中的引号。它可能有用。
URL url = new URL("nextHref");
应该是,
URL url = new URL(nextHref);
答案 1 :(得分: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();