Skip to content

Make Selenium Easy

And Keep It That Way

  • Home
  • Share
  • Toggle search form

How To Handle A Web Table In Selenium Webdriver | Make Selenium Easy

Posted on 12/06/2018 By admin

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 

 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

. Generally first row of table is header. We can retrieve header name and print them using below code:

// Printing table header of a web table assuming first row as header
List allHeadersOfTable= driver.findElements(By.xpath("//table[@name='BookTable']/tbody/tr[1]/th"));
System.out.println("Headers in table are below:");
System.out.println("Total headers found: "+allHeadersOfTable.size());
for(WebElement header:allHeadersOfTable)
{
        System.out.println(header.getText());
}
Output:
Headers in table are below:
Total headers found: 4
BookName
Author
Subject
Price

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:

// Printing table header of a web table assuming no information about header row
List allHeadersOfTable1= driver.findElements(By.xpath("//table[@name='BookTable']/tbody/tr/th"));
System.out.println("Headers in table are below:");
System.out.println("Total headers found: "+allHeadersOfTable1.size());
for(WebElement header:allHeadersOfTable1)
{
        System.out.println(header.getText());
}
Output:
Headers in table are below:
Total headers found: 4
BookName
Author
Subject
Price

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

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

// Finding number of rows in a web table. We need to exclude header to get actual number of data rows
List allRows= driver.findElements(By.xpath("//table[@name='BookTable']/tbody/tr"));
System.out.println("Total data rows found in table:"+ (allRows.size()-1));
Total data rows found in table:6

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:

// Find number of columns in each row
List allRows= driver.findElements(By.xpath("//table[@name='BookTable']/tbody/tr"));
// We will start from 2nd row as 1st row is header
for(int i=2;i
tag.

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

    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.

    HTMLTable.html:

    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:

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

with
Uncategorized

Post navigation

Previous Post: Advanced Selenium Concepts – Custom Select Class – Select Value Containing Specific Text in Drop down | Make Selenium Easy
Next Post: Frequently Asked Java Program 24: Java Program to Capitalize First Character of Each Word in a String Sentence | Make Selenium Easy

Related Posts

Problems in Parallel Execution With Static WebDriver Uncategorized
TestNG Tutorials – Page 5 – Make Selenium Easy Uncategorized
Postman Tutorial Part 53 – Extracting Value From XML Response in Postman Uncategorized
Postman Tutorial Part 9 – DIfference Between Put and Patch HTTP Methods Uncategorized
March 28, 2019 – Make Selenium Easy Uncategorized
Part 3: Usages Of Javascripts In Selenium: Problem You Might Face While Executing Javascript Commands In Selenium 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