Is it possible to find broken links on a webpage using Selenium Webdriver

0 votes
May 17, 2019 in Selenium by Pragya
1,912 views

2 answers to this question.

0 votes

Hi Pragya, this piece of code worked for me to find broken links in a webpage:

package brokenLinksPkg;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Iterator;
import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class BrokenLinks {
    
    private static WebDriver driver = null;

    public static void main(String[] args) {
        
        String homePage = "https://www.quora.com/";
        String url = "";
        HttpURLConnection huc = null;
        int respCode = 200;
        
        driver = new ChromeDriver();
        
        driver.manage().window().maximize();
        
        driver.get(homePage);
        
        List<WebElement> links = driver.findElements(By.tagName("a"));
        
        Iterator<WebElement> it = links.iterator();
        
        while(it.hasNext()){
            
            url = it.next().getAttribute("href");
            
            System.out.println(url);
        
            if(url == null || url.isEmpty()){
System.out.println("URL is either not configured for anchor tag or it is empty");
                continue;
            }
            
            if(!url.startsWith(homePage)){
                System.out.println("URL belongs to another domain, skipping it.");
                continue;
            }
            
            try {
                huc = (HttpURLConnection)(new URL(url).openConnection());
                
                huc.setRequestMethod("HEAD");
                
                huc.connect();
                
                respCode = huc.getResponseCode();
                
                if(respCode >= 400){
                    System.out.println(url+" is a broken link");
                }
                else{
                    System.out.println(url+" is a valid link");
                }
                    
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        
        driver.quit();

    }
}
answered May 17, 2019 by Abha
• 28,140 points
+3 votes

Yes it is possible to find broken links on a website using selenium code. This article contains detail code to fetch links, broken links 
http://mundrisoft.com/tech-bytes/code-to-identify-all-web-broken-links-of-a-website/

answered Jul 25, 2019 by anonymous
Hi, thank you for sharing the solution for finding broken links. But it would be great if you could share your code here on the portal, as many people finds it easier.

Related Questions In Selenium

+1 vote
2 answers

Is it possible to scroll down in a webpage using selenium webdriver programmed on python?

I using next code for facebook for ...READ MORE

answered May 16, 2019 in Selenium by mslavikas@gmail.com
25,552 views
0 votes
1 answer
0 votes
1 answer
0 votes
2 answers

Finding WebDriver element with Class Name in java

The better way to handle this element ...READ MORE

answered Apr 10, 2018 in Selenium by nsv999
• 5,500 points
12,616 views
0 votes
2 answers

Problem while using InternetExplorerDriver in Selenium WebDriver

enable trusted connection  in internet explorer by ...READ MORE

answered Aug 31, 2020 in Selenium by Sri
• 3,190 points
8,571 views
0 votes
1 answer

Geo-location microphone camera pop up

To Allow or Block the notification, access using Selenium and you have to ...READ MORE

answered May 11, 2018 in Selenium by Samarpit
• 5,910 points
6,629 views
0 votes
2 answers

How to use such xpath to find web elements

xpath are two types. 1) Absolute XPath:    /html/b ...READ MORE

answered Sep 3, 2020 in Selenium by Sri
• 3,190 points
7,517 views
+10 votes
17 answers

How to automate gmail login process using selenium webdriver in java?

Check the below code: Here is the working ...READ MORE

answered Apr 24, 2018 in Selenium by Vardy
• 2,360 points
193,866 views
0 votes
1 answer

How to count words in notepad File using Selenium

To perform character count. String fPath="D:/MyTextFile.txt"; int fileCharactersCount=Files.readAllBytes(new File(fPath).toPath() ...READ MORE

answered Aug 7, 2019 in Selenium by neerajv
• 170 points
1,845 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP