List links = driver.findElements(By.tagName(“a”));
for(int i=0; i<links.size(); i++) {
WebElement element = links.get(i);
String url=element.getAttribute(“href”);
try {
URL link = new URL(url);
HttpURLConnection httpConn =(HttpURLConnection)link.openConnection();
httpConn.setConnectTimeout(2000);
httpConn.connect();
if(httpConn.getResponseCode()== 404) {
String BrLink = url.concat("
");
System.out.println(url);
}
}
catch (Exception e) {
//e.printStackTrace();
}
}
Test Project converted “driver.findElements(By.tagName(“a”)” to one step and element.getAttribute(“href”) as mutliple steps with output showing the links. I need to take broken links as output (BrLink).