Make Selenium Easy

How To Handle A Web Table In Selenium Webdriver

Hello Folks,

In this post we will learn:
1. What is a web table?
2. Types of web tables.
2. Creating a demo web table using html.
3. Print all headers of a web table.
4. Retrieve and print number of rows in a web table.
5. Retrieve number of columns for each row.
6. Retrieve columns based on some conditions.
7. Retrieve last row of table.
8. Retrieve cell value using row and column number.
9. Retrieve column index based on column name.
10. Print all data from table.

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, 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.

Types of web tables:

We can categorized web tables in two parts:

  1. Static web table: Number of rows and columns will be definite. Eg. Table of months, Table of days etc.
  2. Dynamic table: Number of rows and columns will be dynamic. It will be keep on increasing or decreasing based on data. For Eg: Sales table, Student table.

Creating a demo web table using html:

We will  create a web table which displays book name, author, subject and price. HTML code is very easy and self descriptive. You must know how to identify header, rows and columns.


Save above html code with .html extension. I will save it as HTMLTable.html.

When we open above html file in a browser, it will be shown as below:

BookName Author Subject Price
Learn Selenium Amit Selenium 300
Learn Java Mukesh Java 500
Learn JS Animesh Javascript 300
Master In Selenium Mukesh Selenium 3000
Master In Java Amod JAVA 2000
Master In JS Amit Javascript 1000

Handling different scenarios of web table:

1. Print all headers of web table:

Headers of web able is also a row. Header is created using <th> with <tr>. Generally first row of table is header. We can retrieve header name and print them using below code:

In above code, I am using index (/tr[1]) to locate first row of table. I am assuming first row in table will be header. Suppose, if we are not sure which row is header, then we can use below code:


2. Retrieve and print number of rows in a web table:

We can retrieve and print number of rows using below code:

Since total number of rows includes header row, so we need to exclude header row from total number of rows to get actual number of data rows.

3. Find number of columns for each row:

Rows of a dynamic web table can have different number of columns. For example: Row1 has 3 columns while Row2 has 2 columns only. We can retrieve number of columns for each row using below code:

4. Retrieve name of book whose author is Mukesh:

Way 1:

In table, we know 2nd column consists author name. So first we will retrieve author name column and will match by required author name. If it matches hen we will take value from 1st column which is book name.

Way 2:

This way is more efficient. I am using xpath text() method here.

5. List all books whose price is greater than or equal to 1000.

We can achieve this using below code:

6. Print last row of table:

We know number of rows in a dynamic web table will be keep on changing. Take an example of shopping mart. All sale amount are entered in a web table and at end of the day, sum of amount is displayed as a last row of table. So, directly we need to access last row to get total sell amount. We can not use a static row number because rows in table will depend on number of sales. In this case we will use last() method of xpath as shown below:

7. Print total cost of all books listed in table:

We can print sum of cost of all books using below code:

8. Retrieving cell value of specific column of specific row

We can locate a particular cell based on row and column number as shown below:

You can also make a customized method with parameters as shown below:

You can make it more customizing by passing attribute value as parameters. Try that yourself.

9. Finding column index using column name:

You have requirement that you need to find average of cost of books. You are not aware which column has cost details. In this case, you first need to iterate header and match header name with “Price”. If it matches take that column number. Your problem is solve.

10. Printing all data of table:

We can print all data of table using below code:


Complete JAVA CODE:


That’s it guys. A very long post as I tried to cover many cases. I hope it will be useful for handling web tables. If you have any doubts or find a mistake in this post, feel free to comment.

If you like my posts, please like, comment, share and subscribe.

Must refer Frequently Asked JAVA Programs In Interview.



Author: Amod Mahajan

A software Tester who is paid to judge products developed by others. Currently getting paid in American Dollars. Writing technical posts and creating YouTube videos are my hobbies.

13 thoughts on “How To Handle A Web Table In Selenium Webdriver

  1. A very good explanation on tables and also teaching us efficient way of coding.Can you please make post on how to write the web table in an excel.

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.