- Given any web page, we would like to count the number of links on that particular web page.
- We will use Selenium Web driver using Java binding language.
Steps to follow :
- Navigate to the specific webpage(https://www.makeinjava.com).
- Find all the links from the webpage using findElements() function. All the links are associated with the Tag ‘a‘ (anchor tag).
- driver.findElements(By.tagName(“a”)).size() will give the count of number of links on particular web page.
Code: count number of links in a web page (selenium webdriver)
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class CountNoOfLinks {
public static void main(String[] args) {
// Enter path of webdriver
System.setProperty("webdriver.chrome.driver", "C:\\\\work\\\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://www.makeinjava.com");
int numberOfLinks = driver.findElements(By.tagName("a")).size();
// count no of links on page
System.out.println("Number of links on Web Page :" +numberOfLinks );
}
}
Output – display number of web link using selenium (webdriver/automation)
Number of links on Web Page :90