问题描述
我想验证网站上已损坏的链接,我正在使用此代码:
I want to verify broken links on a website and I am using this code:
public static int invalidLink;
String currentLink;
String temp;
public static void main(String[] args) throws IOException {
// Launch The Browser
WebDriver driver = new FirefoxDriver();
// Enter URL
driver.get("http://www.applicoinc.com");
// Get all the links URL
List<WebElement> ele = driver.findElements(By.tagName("a"));
System.out.println("size:" + ele.size());
boolean isValid = false;
for (int i = 0; i < ele.size(); i++) {
isValid = getResponseCode(ele.get(i).getAttribute("href"));
if (isValid) {
System.out.println("ValidLinks:" + ele.get(i).getAttribute("href"));
driver.get(ele.get(i).getAttribute("href"));
List<WebElement> ele1 = driver.findElements(By.tagName("a"));
System.out.println("InsideSize:" + ele1.size());
for (int j=0; j<ele1.size(); j++){
isValid = getResponseCode(ele.get(j).getAttribute("href"));
if (isValid) {
System.out.println("ValidLinks:" + ele.get(j).getAttribute("href"));
}
else{
System.out.println("InvalidLinks:"+ ele.get(j).getAttribute("href"));
}
}
} else {
System.out.println("InvalidLinks:"
+ ele.get(i).getAttribute("href"));
}
}
}
}
public static boolean getResponseCode(String urlString) {
boolean isValid = false;
try {
URL u = new URL(urlString);
HttpURLConnection h = (HttpURLConnection) u.openConnection();
h.setRequestMethod("GET");
h.connect();
System.out.println(h.getResponseCode());
if (h.getResponseCode() != 404) {
isValid = true;
}
} catch (Exception e) {
}
return isValid;
}
}
推荐答案
我会保持它返回一个int,只是让MalformedURLException成为一个特例,返回-1。
I would keep it returning an int, and just have the MalformedURLException be a special case, returning -1.
public static int getResponseCode(String urlString) {
try {
URL u = new URL(urlString);
HttpURLConnection h = (HttpURLConnection) u.openConnection();
h.setRequestMethod("GET");
h.connect();
return h.getResponseCode();
} catch (MalformedURLException e) {
return -1;
}
}
编辑:看来你坚持使用布尔值方法,正如我之前所说的那样有它的局限性,但是应该可以用于演示目的。
It seems you're sticking with the boolean approach, as I said before this has it's limitations but should work ok for demonstartion purposes.
没有理由第二次找到你所采用的方法来找到所有元素。试试这个:
There is no reason to find all elements a second time taking the approach you have. Try this:
// Get all the links
List<WebElement> ele = driver.findElements(By.tagName("a"));
System.out.println("size:" + ele.size());
boolean isValid = false;
for (int i = 0; i < ele.size(); i++) {
string nextHref = ele.get(i).getAttribute("href");
isValid = getResponseCode(nextHref);
if (isValid) {
System.out.println("Valid Link:" + nextHref);
}
else {
System.out.println("INVALID Link:" + nextHref);
}
}
这是未经测试的代码,所以如果它不起作用,请提供更多细节,而不仅仅是说'它不起作用',提供输出和放大器。任何堆栈跟踪/错误消息(如果可能)。干杯
This is untested code, so if it does not work, please provide more detail than just saying 'it doesn't work', provide output & any stack traces/error messages if possible. Cheers
这篇关于如何使用Selenium WebDriver和Java查找损坏的链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!