Automation Using Selenium Webdriver
Showing posts with label Find All Links In a page. Show all posts
Showing posts with label Find All Links In a page. Show all posts

Monday 24 October 2016

Find All Links In a page

Find All Links
Testers might be in a situation to find all the links on a website. We can easily
do so by finding all elements with the Tag Name "a", as we know that for any
link reference in HTML, we need to use "a" (anchor) tag.

import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
public class getalllinks
{
public static void main(String[] args)
{
WebDriver driver = new FirefoxDriver();
driver.navigate().to("http://www.calculator.net");
java.util.List<WebElement> links =
driver.findElements(By.tagName("a"));
System.out.println("Number of Links in the Page is " +
links.size());
for (int i = 1; i<=links.size(); i=i+1)
{
System.out.println("Name of Link# " + i - +
links.get(i).getText());
}
}
}
Output
The output of the script would be thrown to the console as shown below. Though
there are 65 links, only partial output is shown below.