Find total number of Links on a Webpage using Selenium

package SeleniumLearn.Com;

import org.testng.annotations.Test;
import org.testng.annotations.BeforeTest;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterTest;

public class TotalNumberLinks{
     public WebDriver driver;
@Test 
       public void TotalNumberofLinks() throws InterruptedException {
        driver.get("http://www.seleniumlearn.com");
        driver.manage().timeouts().implicitlyWait(9, TimeUnit.SECONDS);
        driver.manage().window().maximize();
        List<WebElement> list=driver.findElements(By.tagName("a"));
        System.out.println("Total number of links:"+ list.size());

         }

@BeforeTest
  public void beforeTest() {
      driver= new FirefoxDriver();
      driver.manage().window().maximize();
  }

@AfterTest
  public void afterTest() {
  }
}

 

Output :- Total number of links : 165

Tags: