Capture and Navigate All The Links on Webpage Using Selenium Webdriver


In case of Sanity Check we can use this Code to check whether all links are functional or not.
Its so simple to capture links and navigate

public static void navigateToAllLinks() throws InterruptedException {
List<WebElement> linksize = driver.findElements(By.tagName(“a”));
int linksCount = linksize.size();
System.out.println(“Total no of links Available: ” + linksCount);
String[] links = new String[linksCount];
System.out.println(“List of links Available: “);
// print all the links from webpage
for (int i = 0; i < linksCount; i++) {
links[i] = linksize.get(i).getAttribute(“href”);
System.out.println(linksize.get(i).getAttribute(“href”));
}
// navigate to each Link on the webpage
for (int i = 0; i < linksCount; i++) {
driver.navigate().to(links[i]);
System.out.println(driver.getTitle());

}
}

Advertisements

3 thoughts on “Capture and Navigate All The Links on Webpage Using Selenium Webdriver

  1. malapatisivaramprasad June 4, 2015 / 11:02 am

    I don’t think we need test script to check dead links in a website why don’t we use chrome n Firefox addons like link checker

    Thanks for sharing with community anyway

    Like

    • charan163 June 4, 2015 / 11:06 am

      In case of Sanity Check we have to check all the links are navigating to right page or not.Link checker displays only which link was broken.Thank you !
      .

      Like

  2. Anonymous June 4, 2015 / 10:03 pm

    yes link checker just tells the link was broken.. we cant test the navigation functionality

    Like

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s