[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
findElement(By)
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.
findElements(By)
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.
“abc”
“abc”
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
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:
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 32 33 34 35 36 37 38 39 40 41 |
import java.io.IOException; 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.firefox.FirefoxDriver; public class BrokenLinks { private static WebDriver driver = null; public static void main(String[] args) { HttpURLConnection httpURLConnection = null; System.getProperty("webdriver.gecko.driver","geckodriver.exe"); driver = new FirefoxDriver(); driver.manage().window().maximize(); driver.get("http://total-qa.com"); List<WebElement> links = driver.findElements(By.tagName("a")); for(int i = 0 ; i < links.size();i++) { String resourceUrl = links.get(i).getAttribute("href"); try { httpURLConnection = (HttpURLConnection)(new URL(resourceUrl).openConnection()); httpURLConnection.connect(); System.out.println("Valid url :: " +resourceUrl ); System.out.println("Response Code::" +httpURLConnection.getResponseCode()); //200 for Valid HyperLinks } catch (IOException e) { e.printStackTrace(); System.out.println("Broken url :: " +resourceUrl ); } } driver.quit(); } } |
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.