Selenium - Find all Links


Advertisements

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.

Example

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.

Selenium IDE 91
selenium_user_interactions.htm
Advertisements