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


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.

Selenium IDE 91