Find All Links On The Page using Selenium

  1. import java.util.List;  
  2. import org.openqa.selenium.By;  
  3. import org.openqa.selenium.WebDriver;  
  4. import org.openqa.selenium.WebElement;  
  5. import org.openqa.selenium.firefox.FirefoxDriver;  
  6.   
  7. public class GetPageLinks {  
  8.   
  9.     public static void main(String[] args) {  
  10.         WebDriver driver = new FirefoxDriver();  
  11.         driver.get("http://www.c-sharpcorner.com");  
  12.         List<WebElement> no = driver.findElements(By.tagName("a"));   
  13.         int numberoflinks = no.size();   
  14.         System.out.println("Total number of links available is: "+numberoflinks);   
  15.         for(WebElement pagelink : no) {   
  16.             String linktext = pagelink.getText();   
  17.             String link = pagelink.getAttribute("href");   
  18.             System.out.println(linktext+" -> "+link);  
  19.         }  
  20.     }  

Output
 
Total number of links available is: 342 and also print their respective links.