Overview of This Post-

Hello Readers, In this blog post, we will see how to use selenium in finding status of various links present over a web page.

HTTP response status codes indicate whether a specific HTTPRequest has been successfully completed. Responses are grouped in five classes: informational responses, successful responses, redirects, client errors, and servers errors. Status of various code is defined after below selenium code.

To identify various hyperlink in a webpage, manual validation can be done though manual efforts will be more, it can take hours as manually each and every link needs to be opened in Browser which takes its own time.

However if we use selenium along with HTTPUrlConnection class same can be done in a matter of few minutes. This saves a lot of time as after opening browser httpconnection checks response code without opening the same in browser and only by hitting the specified url using http connection only.

Please have a look into below code- Please find below steps, what this code is doing-

1- Opening WebUrl in Chrome browser.
2- Creating List of all anchor tag .
3- Extracting href from all anchor tag objects and sending to another method which is using HTTPUrlConenction class to validate HTTP Response code.


Refer below code for broken link validation which is calling HTTPLinkstatusChecker method

package Selenium_Example;
import java.net.HttpURLConnection;
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.chrome.ChromeDriver;

public class LinkValidation {

* @param args
public static void main(String[] args)
// TODO Auto-generated method stub E:\chromedriver_win32
String exePath = “E:\\chromedriver_win32\\chromedriver.exe”;
System.setProperty(“webdriver.chrome.driver”, exePath);
WebDriver driver=new ChromeDriver();
List MyLinks=driver.findElements(By.tagName(“a”));
System.out.println(“MyLink size–>”+MyLinks.size());

for(int i=0;i MyLinks.size();i++)
WebElement ele= MyLinks.get(i);
String url=ele.getAttribute(“href”);

Method 2- This method needs to be called where-ever broken links needs to be validated

Below code is for HTTPLinkStatusChecker, which validate all hyperlinks available on webpage one by one. This method is being called in above-written class

public static void HTTPLinkstatusChecker(String linkUrl)
URL url = new URL(linkUrl);
HttpURLConnection objhttpURLConnection=(HttpURLConnection)url.openConnection();
System.out.println(linkUrl+” Error*-* +”+objhttpURLConnection.getResponseCode());
//This syntax will print http response code.
System.out.println(linkUrl+” Perfect*-* “+objhttpURLConnection.getResponseCode());
//This syntax will print http response message
System.out.println(linkUrl+” *-* “+httpURLConnect.getResponseMessage());
} catch (Exception e) {

Please find below output of above syntax-

Starting ChromeDriver 2.43.600210 (68dcf5eebde37173d4027fa8635e332711d2874a) on port 39785
Only local connections are allowed.
Oct 20, 2018 8:29:28 AM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: OSS
MyLink size–>51
https://www.facebook.com/# Perfect*-* 200
https://www.facebook.com/# Perfect*-* 200
https://www.facebook.com/ Perfect*-* 200
https://www.facebook.com/recover/initiate?lwv=110&ars=royal_blue_bar Perfect*-* 200
https://www.facebook.com/# Perfect*-* 200
https://www.facebook.com/legal/terms/update Perfect*-* 200
https://www.facebook.com/about/privacy/update Perfect*-* 200
https://www.facebook.com/policies/cookies/ Perfect*-* 200
https://www.facebook.com/# Perfect*-* 200
https://www.facebook.com/# Perfect*-* 200
https://www.facebook.com/pages/create/?ref_type=registration_form Perfect*-* 200
https://hi-in.facebook.com/ Perfect*-* 200
https://pa-in.facebook.com/ Perfect*-* 200
https://ur-pk.facebook.com/ Perfect*-* 200
https://ta-in.facebook.com/ Perfect*-* 200
https://bn-in.facebook.com/ Perfect*-* 200
https://mr-in.facebook.com/ Perfect*-* 200
https://te-in.facebook.com/ Perfect*-* 200
https://gu-in.facebook.com/ Perfect*-* 200
https://kn-in.facebook.com/ Perfect*-* 200
https://ml-in.facebook.com/ Perfect*-* 200
https://www.facebook.com/# Perfect*-* 200
https://www.facebook.com/r.php Perfect*-* 200
https://www.facebook.com/login/ Perfect*-* 200
https://messenger.com/ Perfect*-* 200
https://www.facebook.com/lite/ Perfect*-* 200
https://www.facebook.com/mobile/?ref=pf Perfect*-* 200
https://www.facebook.com/find-friends?ref=pf Perfect*-* 200
https://www.facebook.com/directory/people/ Perfect*-* 200
https://www.facebook.com/directory/pages/ Perfect*-* 200
https://www.facebook.com/directory/videos/interests/ Perfect*-* 200
https://www.facebook.com/places/ Perfect*-* 200
https://www.facebook.com/games/ Perfect*-* 200
https://www.facebook.com/directory/places/ Perfect*-* 200
https://www.facebook.com/directory/marketplace/ Perfect*-* 200
https://www.facebook.com/directory/groups/ Perfect*-* 200
https://l.facebook.com/l.php?u=https%3A%2F%2Finstagram.com%2F&h=AT20ojlSeFJ9pPU3JM2V4NrzN6WbzErq_VYGqnUKbOSQ9ziojm62emsQag2EToBRrAX6omqoT2Oew1Ix7422vi-m2ALK_fec5dhKy2F5Y8n7ohecWyQeSM-Cj1iP2d8L24TVSfOMxu–RPPklaUx Perfect*-* 200
https://www.facebook.com/local/lists/245019872666104/ Perfect*-* 200
https://www.facebook.com/facebook Perfect*-* 200
https://www.facebook.com/ad_campaign/landing.php?placement=pflo&campaign_id=402047449186&extra_1=auto Perfect*-* 200
https://www.facebook.com/pages/create/?ref_type=sitefooter Perfect*-* 200
https://developers.facebook.com/?ref=pf Perfect*-* 200
https://www.facebook.com/careers/?ref=pf Perfect*-* 200
https://www.facebook.com/privacy/explanation Perfect*-* 200
https://www.facebook.com/policies/cookies/ Perfect*-* 200
https://www.facebook.com/help/568137493302217 Perfect*-* 200
https://www.facebook.com/policies?ref=pf Perfect*-* 200
https://www.facebook.com/help/2687943754764396 Perfect*-* 200
https://www.facebook.com/help/www/1573156092981768/ Perfect*-* 200
https://www.facebook.com/help/?ref=pf Perfect*-* 200
https://www.facebook.com/settings Perfect*-* 200
https://www.facebook.com/allactivity?privacy_source=activity_log_top_menu Error*-* +404

Above code can be customized as per the need of link validation, Like-

1- If only 1xx condition is required to check or 3xx, 4xx and 5xx etc, which every condition is required, same values can be filled in if block, if multiple requests are need to check like we need to fail all 301 and 404 then use and condition, like below-

if(objhttpURLConnection.getResponseCode()==301 AND objhttpURLConnection.getResponseCode() 404)
Assert.fail("Links are not responding accordingly.")

To understand in detail about various other status code with their meaning

please find below list of all possible status code and messages, Above code can be used to identify any of the below available HTTP status code.

1xx (Informational):

100 Continue
101 Switching protocol
102 Processing
103 Early Hints

2xx (Successful):

200 OK
201 Created
202 Accepted
203 Non-Authoritative Information (since HTTP/1.1)
204 No content
205 Reset Content
206 Partial content
207 Multi Status
208 Already Reported
226 IM Used

3xx (Redirection):

300 Multiple Choices
301 Moved Permanently
302 Found
303 See Other
304 Not Modified
305 Use Proxy
306 Switch Proxy
307 Temporary Redirect
308 Permanent Redirect

4xx (Client Error):

401 Bad request
402 Unauthorised
403 Forbidded
404 Not Found
405 Method not allowed
406 Not Acceptable
407 Proxy Authentication Required
408 Request Timeout
409 Conflict
410 Gone
411 Length required
412 Precondition failed
413 Payload too large
414 URL too long
415 Unsupported media type
416 Range not satisfiable
417 Expectation failed
418 I m Teapot
421 Misdirect Request
422 Unproccessable Entity
423 Locked
424 Failed dependency
426 Upgrade Required
428 Precondition required
429 Too Many Request
431 Request header field so large
451 Unavailable for legal reasons

5xx (Server Error):

501 Internal Server Error
502 Not Implemented
503 Bad Gateway
504 Gateway Timeout
505 HTTP Version not supported
506 Variants Also Negotiated
507 Insufficient storage
508 Loop Detected
510 Not Extended
511 Network Authentication Required

Reference of status code taken from -https://en.wikipedia.org/wiki/List_of_HTTP_status_codes