需要专门用于标记的断开链接检查
这是当前可用于其他标签(img,a)但不适用于源代码的代码:
List<String> srctag = new ArrayList<String>();
String homePage = urlname;
String url = "";
HttpURLConnection huc = null;
int respCode = 200;
Dimension dimension= new Dimension(500, 700 );
driver.manage().window().setSize(dimension);
driver.get(homePage);
List<WebElement> links = driver.findElements(By.tagName("source"));
Iterator<WebElement> it = links.iterator();
while(it.hasNext()){
url = it.next().getAttribute("srcset");
System.out.println(url);
if(url == null || url.isEmpty()){
System.out.println("URL is either not configured for anchor tag or it is empty");
continue;
}
try {
huc = (HttpURLConnection)(new URL(url).openConnection());
huc.setRequestMethod("GET");
huc.connect();
respCode = huc.getResponseCode();
if(respCode >= 400){
System.out.println(url+" is a broken link");
srctag.add(url+" is a broken link");
}
else{
System.out.println(url+" is a valid link");
srctag.add(url+" is a valid link");
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
错误:java.net.MalformedURLException:无协议://sample.png在java.net.URL。(未知来源)