Automation Using Selenium Webdriver

Wednesday, 23 November 2016

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());


}
}

No comments:

Post a Comment