Find Broken Links in Selenium

You have seen the approach, How to find the links on the web page, Now you will see, How to Find Broken Links in Selenium. We have several ways to locate the links on the web page. Let’s see the all possible ways to locate the Link on web page-

 

1. linkText() Method – Accessing links using their exact link text is done through the By.linkText() method. If there are more than one links that have the very same link text, this method will only access the first one.

i.e if we have link ‘click here’ then selenium command will be – By.linkText(“click here”).click() ;

The above command will click on the ‘click here’ link on the target page.

 

Please note this method is case sensitive If you will pass the argument like ‘CLICK here’ instead of link text ‘click here’. It will throw an error, No such element found.

 

2. partialLinkText() Method – Accessing links using their partial link text is done through the By.partialLinkText() method. If you specify a partial link text that has multiple matches, only the first match will be accessed.

i.e if we have link ‘click here’ then selenium command will be –By.partialLinkText(“here”).click() ;

The above command will click on the ‘click here’ link on the target page. It is also case sensitive if you pass argument ‘Here’ instead of ‘here’. This will not be able to find out the link and throw a NoSuchElementFound error.

 

Now let’s see how can we find out all available links on the web page or find out the broken link on a web page if any.

 

Find Broken Links on Webpage using Selenium

This is an important Testing scenario, To test all links on the web page to ensure that no broken link is available on the web page. Now the question is how can we do this using selenium.

Let’s first understand, What is Broken Link?

Broken Link is the link, which is pointing to the wrong URL or on click which renders the User on  404 page not found error. An invalid link is Invalid URL and to verify the URL, All you need to do, To hit the URL and verify the Status Code.

Valid URL will always return the status code 200. We have different HTTP status codes which are used for different purposes. You can check here for more information on HTTP Status Codes.

 

To verify all Links on Web Page,  you need to write a selenium script  , To verify this using Selenium we can use HttpsURLConnection library to check status codes of the URLs on a page. If the request was NOT processed correctly, then the HTTP status codes may return any of the above-listed codes but not a 200 status code. We can easily say whether the link is broken or not with status codes.

Now let’s see, How can you find out the response code of the URL. Let’s take code example –

 

In the above Java Code, We have created the class Static, So that we can call the class member without creating the Object of the class. In Java class GetReponseCode  we have implemented the two functions verifyUrl() to verify the URL and getInvalidLinkCount() to count the number of invalid links.

 

Now let’s write a Selenium Script to verify all links on the web page.

 

Let’s understand the above code, In above script, we are opening the web application in @BeforeTest annotation and closing the browser in the @AfterTest annotation.

In getBrokenLinks() TestCase, First we found all anchor tags on the page by using findElements. Then we are extracting the URL using getAttribute(“href“) because all anchor link contains the Text in the href attribute.

After this verifying the URL using verifyURL() function of GetReponseCode class. In the above script, we are verifying the broken links of the Google home page.

 

Try this code on your local machine to find out the broken or empty links on Oyepase or any web page.

0 Comment

Leave a Reply