Make Selenium Easy

MSE-ReadyToUseSeleniumCode – Storing Web Table Data Into List Of Map – Java

Last updated on July 22nd, 2020 at 08:23 am

I started a series of “Ready To Use Selenium Java Code”. In the first post in this series, we will learn “Storing a Web table data in to a List of Map using Selenium-Java.”.

WHAT IS WEB TABLE?

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 <table> 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 <th> tag.
  2. Row(s):It is created using <tr> tag.
  3. Columns(s):It is created using <td> tag.

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 <thead> and remaining row can be grouped as <tbody>. Last row can also be grouped as <tfoot>. An example is shown below:-

If you save above html code as <someName>.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:-

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 ID=amod@gmail.com, Mobile No=12345, Ready To Relocate=Yes},

{ID=2, Name=Mukesh, Age=30, Expereince=8 Years, Location=Bengaluru, Marital Status=Yes, Native=MP, Email ID=mukesh@gmail.com, 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 ID=amod@gmail.com, 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 ID=mukesh@gmail.com, Mobile No=23456, Ready To Relocate=Yes}, …]

Below is Java Code. I have tried explaining code well. If you face any issue in understanding please send me a mail or comment. There may be more optimal way of doing the same. I welcome all refactored code as comment on post.

Output:-

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

You can find all Selenium related post here.
You can find all API manual and automation related posts here.
You can find frequently asked Java Programs here.

Author: Amod Mahajan

My name is Amod Mahajan and I am an IT employee with 6+ years of experience in Software testing and staying in Bengaluru. My area of interest is Automation testing. I started from basics and went through so many selenium tutorials. Thanks to Mukesh Otwani as his tutorials are easy and cover basics to advance. I have habit of exploring concepts by deep diving. I used to make notes. I thought of sharing my knowledge through posts and now I am here. #KeepLearning #ShareLearning

2 thoughts on “MSE-ReadyToUseSeleniumCode – Storing Web Table Data Into List Of Map – Java

  1. Require your help here. As In modern technologies we can move the columns in the webtable,
    1. if i want to print the data on the column bases ( say my requirement is that when i give columnName it should retrieve index of the column or if i give index it should provide columnname vice versa) and print the column values for all the rows or particular rowvalues and verify sorting of values in that column.
    2. If I give a particular value for any column I am able to retrieve the row for that value.
    3. If I give a row number and column number it should retrieve value.
    4. If I give a particular value it should retrieve and print the rownumber and column number or column name associated with it based on match.

    Thanks in advance.

Leave a Reply

Please wait...

Subscribe to new posts to become automation expert

Want to be notified when my new post is published? Get my posts in your inbox.

MSE-ReadyToUseSeleniumCode – Storing Web Table Data Into List Of Map – Java

by Amod Mahajan time to read: 4 min
2
%d bloggers like this: