Skip to content

Make Selenium Easy

And Keep It That Way

  • Home
  • Share
  • Toggle search form

Usage of Java Stream API in Selenium – Retrieving Unique Text From a List Of WebElements

Posted on 02/19/2025 By admin

In previous post, we learnt to Retrieve text from a list of web elements using Java Stream APIs.

You may require just to retrieve unique text only. We used a List in previous post which allows duplicates. We can just use a Set to avoid duplicates.

Java Code:-

package RealtimeValdationAutomationTesting; import java.util.HashSet;
import java.util.Set;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test; import io.github.bonigarcia.wdm.WebDriverManager; public class RetrieveUniqueTextFromListOfElementUsingStreamAPICollect { @Test public void RetrieveTextFromListOfElementWithStremAPI() { // Auto setup of chrome executable file WebDriverManager.chromedriver().setup(); // Launching browser WebDriver driver = new ChromeDriver(); // Load the URL driver.get("http://automationpractice.com/index.php"); // Creating a Set to store only unique names Set uniqueProductName = new HashSet(); // Locating all product names at home page driver.findElements(By.xpath("//ul[contains(@class,'active')]//a[@class='product-name']")) .stream() .forEach(product -> uniqueProductName.add(product.getText())); // Print count of product found System.out.println("Total unique product found : "+uniqueProductName.size()); // Printing product names System.out.println("All product names are : "); uniqueProductName.forEach(name -> System.out.println(name)); // closing the browser driver.quit(); } } 

Output:-

Total unique product found : 5
All product names are : 
Printed Dress
Blouse
Printed Chiffon Dress
Faded Short Sleeve T-shirts
Printed Summer Dress

You must be thinking why a separate post just to say use Set for unique elements. Anyone can think the same. Yes you are correct but here I am going to show you usage of some other aggregate functions of stream APIs.

There is another method in Stream api named “map()”. It returns a stream consisting of the results of applying the given function to the elements of this stream. For example:- Suppose I have an array of int and I want to get square of each element of array. e.g. [1,2,3,4] => [1,4,9,16]. We can achieve the same without iterating explicitly using map.

It returns a stream consisting of the distinct elements of this stream. You can remove duplicates from a source using this method.

It is a terminal operation. We can retrieve the final stream in to a list or a set.

I will show usage of each method in below Selenium script:-

package RealtimeValdationAutomationTesting; import java.util.Set;
import java.util.stream.Collectors; import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test; import io.github.bonigarcia.wdm.WebDriverManager; public class RetrieveUniqueTextFromListOfElementUsingStreamAPICollect { @Test public void RetrieveTextFromListOfElementWithStremAPI() { // Auto setup of chrome executable file WebDriverManager.chromedriver().setup(); // Launching browser WebDriver driver = new ChromeDriver(); // Load the URL driver.get("http://automationpractice.com/index.php"); // Locating all product names at home page /* * map() is used to replace stream with text. If stream consists of {webelement1, webelement2}, * after map operation it will be their inner text {textOfWebelement1, textOfWebElement2}. * Using collect() method to store resultant stream to a set directly to remove duplicates. */ Set uniqueProductNames = driver.findElements(By.xpath("//ul[contains(@class,'active')]//a[@class='product-name']")) .stream() .map(productWebElement -> productWebElement.getText()) .collect(Collectors.toSet()); // Print count of product found System.out.println("Total unique product found : "+uniqueProductNames.size()); // Printing product names System.out.println("All product names are : "); uniqueProductNames.forEach(name -> System.out.println(name)); // closing the browser driver.quit(); } } 

Using distinct():-

package RealtimeValdationAutomationTesting; import java.util.List;
import java.util.Set;
import java.util.stream.Collectors; import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test; import io.github.bonigarcia.wdm.WebDriverManager; public class RetrieveUniqueTextFromListOfElementUsingStreamAPICollect { @Test public void RetrieveTextFromListOfElementWithStremAPI() { // Auto setup of chrome executable file WebDriverManager.chromedriver().setup(); // Launching browser WebDriver driver = new ChromeDriver(); // Load the URL driver.get("http://automationpractice.com/index.php"); // Locating all product names at home page /* * map() is used to replace stream with text. If stream consists of {webelement1, webelement2}, * after map operation it will be their inner text {textOfWebelement1, textOfWebElement2}. * distinct() will remove all duplicates * Using collect() method to store resultant stream to a list. */ List uniqueProductNames = driver.findElements(By.xpath("//ul[contains(@class,'active')]//a[@class='product-name']")) .stream() .map(productWebElement -> productWebElement.getText()) .distinct() .collect(Collectors.toList()); // Print count of product found System.out.println("Total unique product found : "+uniqueProductNames.size()); // Printing product names System.out.println("All product names are : "); uniqueProductNames.forEach(name -> System.out.println(name)); // closing the browser driver.quit(); } } 

Output:-

Total unique product found : 5
All product names are : 
Faded Short Sleeve T-shirts
Blouse
Printed Dress
Printed Summer Dress
Printed Chiffon Dress

If you have any doubt, feel free to comment below.If you like my posts, please like, comment, share and subscribe.#ThanksForReading

#HappyLearning

Uncategorized

Post navigation

Previous Post: Selenium Interview Questions: Why Do Not We Switch Back To Driver After Handling An Alert
Next Post: Protractor Tutorial 7 – NPM – Installing a Package Locally & Globally

Related Posts

Git Tutorial 20 – Untracked File Vs Unstaged File Uncategorized
PassBodyToPostReq – Make Selenium Easy Uncategorized
Advanced Concept of Selenium – Design Patterns in Selenium WebDriver – There is Not Only Page Object Model Design Pattern Uncategorized
How To Filter List Using Java Stream API? Uncategorized
xpathexample – Make Selenium Easy Uncategorized
Groups – Make Selenium Easy Uncategorized

Recent Posts

  • Getting Started with Selenium 4: What Is New and How to Upgrade from Selenium 3
  • Manual Testing
  • Baby Steps To Become Efficient Selenium-Java Automation Tester
  • Features of Selenium 4.0.0 Release – Java Binding
  • Part 1: Handling Drop-down Created Using SELECT Tag In Selenium

Recent Comments

No comments to show.

Archives

  • April 2026
  • April 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • October 2024
  • September 2024
  • August 2024
  • April 2024
  • March 2024
  • February 2024
  • December 2023
  • October 2023
  • August 2023
  • November 2022
  • September 2022
  • August 2022
  • July 2022
  • May 2022
  • March 2022
  • October 2021
  • April 2021
  • March 2021
  • January 2021
  • December 2020
  • October 2020
  • September 2020
  • August 2020
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • September 2019
  • August 2019
  • May 2019
  • December 2018
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • January 2018

Categories

  • Getting Started
  • Uncategorized

Copyright © 2026 Make Selenium Easy.

Powered by PressBook Masonry Dark