Count number of links in a webpage using Selenium

Count number of links in a webpage using Selenium

In this topic you will learn Count number of links in a web page using Selenium which includes list , java.util.list.I have used java.awt.list in List then i am receiving this error message as RemoteWebElement cannot be cast to java.awt.List ,unable to find the solution every one is saying use list to find the size of the links for declared objects,But not able to find the solution at last find the solution as java.util.list i.e you need to use import java.util.list instead of import java.awt.list then only you can write the script as below.

Count number of links in a webpage using Selenium

In this we will use below simple code to count the links with the help of tagname as below.

List lists = (List) driver.findElements(By.tagName("a"));

Here By.tagName i am using why because all links in a webpages are identifying with tag that is the reason i’m using By.tagName(“a”);

ALSO READ  How to take screenshot in Selenium IDE

Let’s see the following example script to Count number of links in a webpage,by this example you will count links for any website.

package com.Payback.Actionconfig;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class Practise {
public static WebDriver driver;
String baseUrl;
@BeforeTest public void setup() {
File Chromedriver = new File("F:\Java_Applications\Zip Files\chromedriver.exe");
System.setProperty("", Chromedriver.getAbsolutePath());
ChromeOptions options = new ChromeOptions();
driver = new ChromeDriver(options);
baseUrl = "";

public void Countlinks() throws InterruptedException {
try {
List lists = (List)
int Linkscount = lists.size();
System.out.println("Number of links in a webpage is " + Linkscount);

} catch (Exception e) {



public void closeBrowser() {
System.out.println("Browser is closed");



In case it is very helpful and knowledgeable please share it as well as provide your valuable suggestions.Thank you for reading my website.

ALSO READ  Selenium Grid Appium configuration Example

Test Results:

Started InternetExplorerDriver server (64-bit)
Listening on port 44910
Only local connections are allowed
Page title of websiteLearn Selenium Automation Testing
Page title is matching with Expected Results – Test Case is PASS
Number of links in a webpage is 52
IE Browser is closed
PASSED: openSite
    Default test
    Tests run: 1, Failures: 0, Skips: 0
Default suite
Total tests run: 1, Failures: 0, Skips: 0


ALSO READ  Selenium Webdriver Using Python - Complete Tutorial Guide

Must Read

Selenium WebDriver Architecture
Verify element is enable in Selenium
Read Data From properties file using Selenium
Launch Firefox Browser using GeckoDriver
Selenium-Testng examples to execute multiple classes
Selenium WebDriver Methods
Generate HTML Reports using Selenium
Mouse Hover action in Selenium
Selenium Limitations

Rajesh K

3 thoughts on “Count number of links in a webpage using Selenium

Comments are closed.