Filtering and counting WebElements

Let's start with a simple test to determine the links displayed on the home page of the sample application. We get all the links from the home page and print their count, followed by the count of links that are visible on the page, as shown in the following code:

@Testpublic void linksTest() {    List<WebElement> links = driver.findElements(By.tagName("a"));    System.out.println("Total Links : " + links.size());    long count = links.stream().filter(item -> item.isDisplayed()).count();    System.out.println("Total Link visible " + count);}

In the preceding code, we used the findElements() method along with By.tagName to get all the links from the home page. However, for finding out the visible links out of them, ...

Get Selenium WebDriver 3 Practical Guide - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.