Best Way to find the broken link for a web page | Selenium Forum
M
Posted on 21/04/2016
Hi,

I have implemented a function to check the broken link using HttpURLConnection class.

Results that I may get

Instead of OK I may get redirected also. This means that user was redirected to a different URL after the request. This is typically a tricky situation because most of the broken links redirect you to a static error page. You should ideally trust only those urls that returned OK, as shown in the results image above, rest should be verified once.

So I am looking for best way to check the broken link.

Note: Code is attached, Please have a look.

Thanks

M
Replied on 23/04/2016

please give the entire code.


M
Replied on 25/04/2016

Please find the attached code.


M
Replied on 26/04/2016

there is no main statement can you please give me your working code.


M
Replied on 26/04/2016

public class BrokenLinks {

static WebDriver driver;

public static List<WebElement> findAllLinks(WebDriver driver)

{
List<WebElement> elementList = new ArrayList();
elementList = driver.findElements(By.tagName("a"));
elementList.addAll(driver.findElements(By.tagName("img")));
List finalList = new ArrayList();
for (WebElement element : elementList)
{
if(element.getAttribute("href") != null)
{
finalList.add(element);
}
}
return finalList;
}
public static String isLinkBroken(URL url) throws Exception{
String response = "";
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
try
{
connection.connect();
response = connection.getResponseMessage();
connection.disconnect();
return response;
}
catch(Exception exp)
{
return exp.getMessage();
}

}
public static void main(String[] args) throws Exception {

driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.get("https://www.charter.com/browse/content/charter-home");
driver.manage().window().maximize();

List<WebElement> allImages = findAllLinks(driver);
System.out.println("Total number of elements found " + allImages.size());
for( WebElement element : allImages){
try
{
System.out.println("URL: " + element.getAttribute("href")+ " returned " + isLinkBroken(new URL(element.getAttribute("href"))));
//System.out.println("URL: " + element.getAttribute("outerhtml")+ " returned " + isLinkBroken(new URL(element.getAttribute("href"))));
}
catch(Exception exp)
{
System.out.println("At " + element.getAttribute("innerHTML") + " Exception occured At; " + exp.getMessage());
}
}
}
}