I want to verify broken links on a website and I am using this code:
package com.automation.test;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Test {
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("any web site");
// 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++) {
// System.out.println(ele.get(i).getAttribute("href"));
isValid = getResponseCode(ele.get(i).getAttribute("href"));
if (isValid) {
System.out.println("ValidLinks:"
+ ele.get(i).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;
}
}
Try the following piece of code another Method::
package com.automation.test;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Test {
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("any web site");
// 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++) {
// System.out.println(ele.get(i).getAttribute("href"));
isValid = getResponseCode(ele.get(i).getAttribute("href"));
if (isValid) {
System.out.println("ValidLinks:"
+ ele.get(i).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;
}
}
I have modified getResponseCode to return boolean values based on whether the url is valid(true) or invalid(false).
Hope this helps you