Count number of links in a webpage using Selenium

In this topic you will learn Count number of links in a webpage 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");

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 java.io.File;

 import org.openqa.selenium.By;
 import org.openqa.selenium.WebDriver;
 import org.openqa.selenium.WebElement;
 import org.openqa.selenium.chrome.ChromeDriver;
 import org.openqa.selenium.chrome.ChromeOptions;
 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("webdriver.chrome.driver",Chromedriver.getAbsolutePath());
 ChromeOptions options = new ChromeOptions();
 options.addArguments("start-maximized");
 driver = new ChromeDriver(options);
 baseUrl="http://www.wikishown.com";

}

//COUNT NO OF LINKS IN A WEBPAGE
 @Test
 public void Countlinks() throws InterruptedException{
 driver.get(baseUrl);
 Thread.sleep(2500);
 try{
 List lists = (List) driver.findElements(By.tagName("a"));
 int Linkscount = lists.size();
 System.out.println("Number of links in a webpage is "+Linkscount);

}catch(Exception e){
 e.printStackTrace();}
     }
  @AfterClass
  public void closeBrowser(){
  System.out.println("Browser is closed");
  driver.close();
  }
}


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

Test Results:

Started InternetExplorerDriver server (64-bit)
2.53.1.0
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
===============================================

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

1 comment: