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

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