[findElements Example]How to find Broken Links in a WebSite using Selenium WebDriver?

[findElements Example]How to find Broken Links in a WebSite using Selenium WebDriver?

In Web Applications it is important to identify the Broken Links. As manual Tester when we click on hyper link if we get HTTP_404 page not found. Then it is obvious that the page is broken. It would be good to find all the HTTP Error Codes as well https://en.wikipedia.org/wiki/List_of_HTTP_status_codes.
There are different ways of identify the broken links manually using the following Add-on:
LinkChecker Addon for Firefox which helps to identify the broken links in web page opened in Firefox.
In order to identify all the links in a webpage we can use findElements() method which returns the List <WebElement>

Differences between FindElements and FindElement in Selenium WebDriver
1. WebElement e = driver.findElement(By.name(“username”));
findElement(By) returns an WebElement as a return value.
2. findElement(By) throws an exception if it is unable to find an Element.
WebElement e =driver.findElement(By.name(“abc”));
NoSuchElementException is an RunTimeException/Unchecked Exception
3. findElement(By) always selects the first one in the list of matching WebElements.

1. findElements(By) returns an List of WebElements
Details About List:
Click on this link to know more about Collections: java.util.Collection

List is available under java.util package.
List is mainly useful to store multiple objects which are duplicates as well.
WebElements which hare having same properties are known as duplicates.
size() – number of the objects available in list
get(int index)- helps in retrieving the particular object
int a[]={1,2,3,4,5};
a[0]// 0 is the index value in Arrays and it 1 in Xpath.

2.findElements(By) returns an empty list if its unable to find an element
List list = driver.findElements(By.name(“abc”));
Empty List means list.size() is zero.
3. findElements(By) selects all the matching webElements and returns a list.

Logic to Identify the Broken Links in a WebPage:

Therefore we can find all the Broken Links in a WebPage using HTTPURLConnection and findElements method in Selenium WebDriver.
Hope this tutorial helps you in checking Broken links using selenium webdriver.

Leave a Reply

Your email address will not be published. Required fields are marked *