Handle Dynamic Web Tables In Selenium WebDriver

Validations of web table content are inevitable in automation testing because they represent data that is either inserted into the database via user input or they represent some information. on the web page. The data of a Web Table can either be static or dynamic in nature. In this article, I will show you how to handle the web tables in Selenium WebDriver to validate its content and perform various useful operations around it.

Before proceeding further let’s try to understand a Web Table.

Web Tables in Selenium WebDriver?

Likewise other web elements a web table is also a web table. A web table consists of rows, columns, and table-cell which contain the actual table data. A web table starts with a <table> tag and consists of the following HTML tags.

  • <tbody> – Indicates body of the table
  • < th > – Indicates heading of the table
  • < tr > – Represents a row in a table
  • < td > – Represents a column in a table

The following sample HTML code will give you a fair idea about the HTML structure of a web table.

Dynamic Web Tables in Selenium WebDriver

The web table will look something like this.

ItemPriceQuantity
Shoe3000.0050
Jacket5000.0020

How to Get Web Table Headers in Selenium WebDriver?

We will refer to the following screenshot and the DOM structure.

Dynamic-Web-Table-in-Selenium-WebDriver-2

The first cell value of the header can be retrieved using the following XPath.

How to Get a Cell Value of a Web Table in Selenium WebDriver?

I am going to show you an example of retrieving table cells for the first row and second column of a web table.
Dynamic Web Table get cell value in Selenium WebDriver

How to Get Row Numbers of a Table in Selenium WebDriver?

It’s quite an easier thing to get row numbers of a table in Selenium WebDriver.You can use the below Selenium Code to get the row numbers. I have shown two different ways to get the row numbers of a table.

How to Get Column Numbers/Count of a Table in Selenium WebDriver?

It’s not necessary that the developer would declare the table header using <th> Tag. So I will show you two ways to get table column numbers when the <th> tag is available and when it is not available.

Get Table Column numbers/count using Table Header Tag Object

have shown two different ways to get the row numbers of a table.

Get Table column numbers/count using Table Row Tag Object

It’s quite an easier thing to get row numbers of a table in Selenium WebDriver.You can use the below Selenium Code to get the row numbers. I have shown two different ways to get the row numbers of a table.

How to Handle Dynamic WebTables in Selenium Webdriver?

So far you have learned all the operations that can be performed on a web table in Selenium Webdriver. Now, here comes the dynamic table. Dynamic tables no longer required any special care. The only difference between a static table and a dynamic table is that the content of the static table does not change whereas the content of the dynamic table keeps on changing.

Dynamic tables might have dynamic links, text, images and etc on the table cells. The row numbers might increase or decrease after inserting and deleting some values. If you have to click any link or image on a dynamic table or set a value in the textbox that is inside a table cell, you can do these simply by locating the object by any of the locators like XPath, CssSelector, etc.

The challenges would come in scenarios like after entering some value on a page a new record gets inserted in the last row of the table and you will have to validate that. There could be a scenario wherein a record for a particular ID or serial number has been deleted from the table and you may have to check that record no longer exists on the table. So how to validate such things.

The solution is quite simple. You should first get the row and column count of the table and then iterate the entire table to validate whether the required record exists on the table or not.

How to Iterate a table in Selenium WebDriver?

In this example, I will iterate all the cells of the table and print them. You can use the same thing and tweak the logic according to your requirement.

Output

Printing values for Row Number: 1
Table Cell Values for row column (1,1): Shoe
Table Cell Values for row column (1,2): 3000.00
Table Cell Values for row column (1,3): 50
Printing values for Row Number: 2
Table Cell Values for row column (2,1): Jacket
Table Cell Values for row column (2,2): 5000.00
Table Cell Values for row column (2,3): 20

How to Get Row Number for a Text in a Table in Selenium WebDriver?

In this example, I will show you how to find a required row number having a specific text(here we will look for Jacket) in any of the cells of that particular row.

Output

Required Row Number for text <<Jacket>> is: 2

I would suggest you understand the code and write your own methods that will accept a String value as a parameter and return the row number of the particular text so that you can just call this method whenever you required in your actual project work.

Recommended Posts

Leave a Reply