November 12, 2019

How to check all broken links in a web page using Selenium Java

  November 12, 2019
To check if all the links inside a web page are working fine or not, at first we need to get all the links inside the web page.
To do this , first extract all the elements with anchor tag (findElements(By.tagName("a")) and return it as a List.

1
2
3
4
5
 public ArrayList<WebElement> GetAllLinks(){
  
  List <WebElement> links = client.objWebDriver.findElements(By.tagName("a")); 
  return (ArrayList<WebElement>) (links);
 }

Now you got all the link type elements on the web page. In order to send an HTTP request to a link, we need to get "href" property (URL) of each element and get it to a String List.


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
 public ArrayList<String> GetAllURLS(){
  
  List <String> links = new ArrayList<>();
  
  for(WebElement elem :client.objWebDriver.findElements(By.tagName("a")))
  
  {
   links.add(elem.getAttribute("href"));
  }

  return (ArrayList<String>) (links);
 
 }

As the next step, we need to send HTTP requests to each of these URLs and verify the response to identify the broken links.

We can pass the same String List we created to the below method to achieve this.


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
 public void VerifyResponse(List<String> links) {

  for (String url : links) {

   String conOut = "";
   int responseCode = 0;
   try {
    URL siteURL = new URL(url);
    HttpURLConnection connection = (HttpURLConnection) siteURL.openConnection();
    connection.setRequestMethod("GET");
    // connection.setInstanceFollowRedirects( false );
    connection.setConnectTimeout(6000);
    connection.connect();

    responseCode = connection.getResponseCode();
    if (responseCode == 200 || responseCode == 301 || responseCode == 302) {
     conOut = "-> Green <-\t" + "Code: " + responseCode + " - " + connection.getResponseMessage();

    } else {
     conOut = "-> Yellow <-\t" + "Code: " + responseCode + " - " + connection.getResponseMessage();
    }
   } catch (Exception e) {
    conOut = "-> Red <-\t" + "Exception: " + e.getMessage();

   }

   String result = url + "   Status: " + conOut;
   System.out.println(result);
  }

 }

If you come across any SSL certificate issue, go through the following link to find the fix
Disable SSL verification while using HttpsURLConnection - Java
logoblog

Thanks for reading How to check all broken links in a web page using Selenium Java

Previous
« Prev Post

No comments:

Post a Comment

Bookmark this website for more workarounds and issue fixes.

Verify Zip file contents without extracting using C# + Selenium

While doing automation testing, we may get a scenario where we need to download a zip file and to validate the contents inside it. One way t...