Checkout our demo site to practice selenium https://magento.softwaretestingboard.com/

0 like 0 dislike
1.1k views
by The go-to Tester (324 points)
How to find broken links/Images from page using selenium Webdriver?
Finding each and every link from page and verifying It manually will take lots of time. So i am looking for soluation?
Please explain with code and example.

1 Answer

0 like 0 dislike
by The go-to Tester (181 points)
selected by
 
Best answer

You can try below code.

 

public static void main(String[] args) {
 
WebDriver driver = new FirefoxDriver();
 
List<String> brokenLinks = getBrokenURLs(driver, "http://mayurshah.in", 2, new ArrayList<String>());
for(String brokenLink : brokenLinks){
System.out.println(brokenLink);
}
 
 
}
public static List<String> getBrokenURLs(WebDriver driver, String appURL, int depth, List<String> links){
{
driver.navigate().to(appURL);
System.out.println("Depth is: " + depth);
while(depth > 0){
List<WebElement> linkElems = driver.findElements(By.tagName("a"));
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 ;
}
 
 
In above code, I am first getting list of URLs from the first page. Now I am navigating to the first link of the IInd page and getting all URLs, this way I will keep on storing all URL by going to each page one by one, till depth is mentioned.
 
After collecting all URLs, I will verify validity of each URL one by one and return List of URLs with 404 page.
 
Hope that helps!
by
moved by
It is not working. Could you please let me know whether it is working on your machine
by Master (1.2k points)
@ravi, I suggest you create a new question through ask page, https://softwaretestingboard.com/qna/ask

Kindly provide the error that you are facing.


This site is for software testing professionals, where you can ask all your questions and get answers from 1300+ masters of the profession. Click here to submit yours now!

...