Skip to content

Make Selenium Easy

And Keep It That Way

  • Home
  • Share
  • Toggle search form

MSE-ReadyToUseSeleniumCode – Storing Web Table Data Into a List Of Maps Using Java Stream APIs

Posted on 03/21/2025 By admin

I started a series of “Ready To Use Selenium Java Code”. In last post we have already seen a detailed Java code to store a web table data in to List of Maps. In this post, we will do the same stuff in more optimal way using Java Stream APIs.

A table is made of rows and columns. When we create a table for a web page, that is called as a web table. In HTML, a web table is created using 

 tag. Web table is a HTML structure for creating rows and columns on a Web page.

A web table can consists below parts:

  1. Header(s): It is created using
tag.

  • Row(s):It is created using tag.Columns(s):It is created using

    A typical web table’s first row is marked as header and from second row onward, it stores values. Table headers can be grouped using and remaining row can be grouped as . Last row can also be grouped as . An example is shown below:-

    tag.
    Item Price
    Rice 100
    Veg 200
    Sum 300
  • If you save above html code as .html and open a web browser, you will see output as below:-

    So, we are going to learn how can we store a web table data in a Java Collection. Let’s start with logic and connect dots.

    Let’s have a bigger web table first. HTML Code is as below:-

     
    ID Name Age Expereince Location Marital Status Native Email ID Mobile No Ready To Relocate
    1 Amod 29 7 Years Bengaluru No Bihar [email protected] 12345 Yes
    2 Mukesh 30 8 Years Bengaluru Yes MP [email protected] 23456 Yes
    3 Animesh 29 5 Years Kolkata No Gujrat [email protected] 98765 No
    4 Aaditya 27 3 Years Bhopal No Bihar [email protected] 34567 No
    5 Prince 36 10 Years Dehradun No WB [email protected] 68768 Yes
    6 Anu 26 3 Years Pune No CG [email protected] 78676 Yes
    7 Sneha 27 5 Years Delhi Yes KA [email protected] 32432 No

    Save above html code in a file with .html extension and open in a web browser. You will see a table as below:-

    Let’s break logic to multiple points first:-

    1.First row is header and remaining are data.

    2. We can store all data in a Java Collection List but identification of data is difficult i.e. which data represents which value.

    3. It is good to store each row in a map where key will be header name and value will be current row cell value. For E.g. [Name=Amod, Age=29…..]

    4. After adding a row in to Map, add that Map in to List. So we will have a List of Map.

    [{ID=1, Name=Amod, Age=29, Expereince=7 Years, Location=Bengaluru, Marital Status=No, Native=Bihar, Email [email protected], Mobile No=12345, Ready To Relocate=Yes},

    {ID=2, Name=Mukesh, Age=30, Expereince=8 Years, Location=Bengaluru, Marital Status=Yes, Native=MP, Email [email protected], Mobile No=23456, Ready To Relocate=Yes}, …]

    5. You can customize in whatever you want. You may want to store in a Map where ID or Name will be key and remaining as value for easy picking.

    [{Amod = {ID=1, Name=Amod, Age=29, Expereince=7 Years, Location=Bengaluru, Marital Status=No, Native=Bihar, Email [email protected], Mobile No=12345, Ready To Relocate=Yes},

    {Mukesh = {ID=2, Name=Mukesh, Age=30, Expereince=8 Years, Location=Bengaluru, Marital Status=Yes, Native=MP, Email [email protected], Mobile No=23456, Ready To Relocate=Yes}, …]

    I have used Stream APIs of Java. It helps in writing logic in shorter form.

    package WebTablesExamples; import java.util.ArrayList;
    import java.util.LinkedHashMap;
    import java.util.List;
    import java.util.stream.Collectors;
    import java.util.stream.IntStream; 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 ReadTableDataInListOfMapUsingStreamAPI { @Test public void readTableDataInListOfMap() { // Browser initialization WebDriverManager.chromedriver().setup(); WebDriver driver = new ChromeDriver(); String fileURL = System.getProperty("user.dir"); driver.get(fileURL + "/src/test/resources/htmlFiles/WebTable.html"); // Each row will be a key value pair. So we will use LinkedHashMap so that order can be retained. // All map will be added to a list. List> allTableData = new ArrayList>(); // Get total headers of table using stream apis, String rowLoc = "//table[@class='tg']//tr"; List headers = driver.findElements(By.xpath("//table[@class='tg']//tr//th")).stream().map(headerEle -> headerEle.getText()).collect(Collectors.toList()); // Getting each row then getting each column of row // skip(1) to skip first row as first row is header row driver.findElements(By.xpath(rowLoc)).stream().skip(1).forEach(row -> { // each cell data of row List rowData = row.findElements(By.tagName("td")).stream().map(columnEle -> columnEle.getText()).collect(Collectors.toList()); // Now iterating both header list and rowData list and putting in to a LinkedHashMap // toMap() has overloaded version.Third argument is to combine duplicate key values. allTableData.add(IntStream.range(0, headers.size()).boxed().collect(Collectors.toMap(i -> headers.get(i), i -> rowData.get(i), (first, second) -> first, LinkedHashMap::new))); }); System.out.println(allTableData); driver.quit(); } } 

    Output:-

    [{ID=1, Name=Amod, Age=29, Expereince=7 Years, Location=Bengaluru, Marital Status=No, Native=Bihar, Email [email protected], Mobile No=12345, Ready To Relocate=Yes}, 
     {ID=2, Name=Mukesh, Age=30, Expereince=8 Years, Location=Bengaluru, Marital Status=Yes, Native=MP, Email [email protected], Mobile No=23456, Ready To Relocate=Yes}, 
     {ID=3, Name=Animesh, Age=29, Expereince=5 Years, Location=Kolkata, Marital Status=No, Native=Gujrat, Email [email protected], Mobile No=98765, Ready To Relocate=No}, 
     {ID=4, Name=Aaditya, Age=27, Expereince=3 Years, Location=Bhopal, Marital Status=No, Native=Bihar, Email [email protected], Mobile No=34567, Ready To Relocate=No}, 
     {ID=5, Name=Prince, Age=36, Expereince=10 Years, Location=Dehradun, Marital Status=No, Native=WB, Email [email protected], Mobile No=68768, Ready To Relocate=Yes}, 
     {ID=6, Name=Anu, Age=26, Expereince=3 Years, Location=Pune, Marital Status=No, Native=CG, Email [email protected], Mobile No=78676, Ready To Relocate=Yes}, 
     {ID=7, Name=Sneha, Age=27, Expereince=5 Years, Location=Delhi, Marital Status=Yes, Native=KA, Email [email protected], Mobile No=32432, Ready To Relocate=No}]

    You may need to edit code based on your web table but logic will be less or more same.

    You can clone code from my git repo.

    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: Handling Bootstrap Tooltips In Selenium Webdriver
Next Post: REST Assured Tutorial 28 – What is Plain Old Java Object (POJO) ?

Related Posts

PassURIPost – Make Selenium Easy Uncategorized
image – Make Selenium Easy Uncategorized
October 12, 2018 – Make Selenium Easy Uncategorized
August 2019 – Make Selenium Easy Uncategorized
July 14, 2018 – Make Selenium Easy Uncategorized
REST Assured Tutorial 43 – Get All Keys From A Nested JSON Object 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