https://www.corteva.ca/en/products-and-solutions/crop-protection/herbicides.html is a broken link on https://www.corteva.ca/en/products-and-solutions/crop-protection.html
However this script is not identifying broken links.
package ca.corteva.tests;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class WBBrokenLinks {
private static WebDriver driver = null;
public static void main(String[] args) throws InterruptedException {
//Instantiating FirefoxDriver
System.setProperty("webdriver.chrome.driver",System.getProperty("user.dir") + "/src/main/java/resources/chromedriver.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();
//Implicit wait for 10 seconds
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
List<String> brokenLinks = getBrokenURLs(driver, "https://www.corteva.ca/", 1, new ArrayList<String>());
for(String brokenLink : brokenLinks){
System.out.println("Broken Link :" + brokenLink);
}
}
public static List<String> getBrokenURLs(WebDriver driver, String appURL, int depth, List<String> links){
{
driver.navigate().to(appURL);
System.out.println("Depth of " + appURL +": " + depth);
while(depth > 0){
//List<WebElement> linkElems = driver.findElements(By.tagName("a"));
List<WebElement> linkElems = driver.findElements(By.xpath("//a[@href!='']"));
//Add all links to links variable
for(WebElement linkElement : linkElems)
if(!links.contains(linkElement))
links.add(linkElement.getAttribute("href"));
for(String link : links)
getBrokenURLs(driver, link, --depth, links);
}
}
return getBrokenURLs(driver, links, new ArrayList<String>()) ;
}
public static List<String> getBrokenURLs(WebDriver driver, List<String> links, List<String> brokenLinks){
{
for(String link : brokenLinks){
driver.navigate().to(link);
if(driver.getTitle().contains("404 Page Not Found")){
brokenLinks.add(link);
}
}
}
return brokenLinks ;
}
}