Home page

Friday, October 18, 2019

Get count of all the links in web page

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class GetLinksCount {

public static void main(String[] args) {

System.setProperty("webdriver.chrome.driver",System.getProperty("user.dir")+"//drivers/chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://www.flipkart.com/");
String newwindow = driver.getWindowHandle();
driver.switchTo().window(newwindow);
driver.findElement(By.xpath("//button[@class='_2AkmmA _29YdH8']")).click();

List<WebElement> links = driver.findElements(By.tagName("a")); //This will store all links in links object.

System.out.println(links.size()); //Use size method to get count

driver.quit();
}

}

1 comment: