Static tables: Data is static i.e. Number of rows and columns are fixed. Dynamic tables: Data is dynamic i.e. Number of rows and columns are NOT fixed.

Now, we will learn how to handle dynamic table in Selenium: Below is an example of a dynamic web table in Selenium for Sales. Based on input date filters, number of rows will get altered. So, it is dynamic in nature.

Handling static table is easy, but handling dynamic tables in Selenium is a little bit difficult as rows and columns are not constant. In this tutorial, you will learn-

Using X-Path to Locate Web Table Elements
Example: Fetch number of rows and columns from Dynamic WebTable
Example: Fetch cell value of a particular row and column of the Dynamic Table
Example: Get Maximum of all the Values in a Column of Dynamic Table
Example: Get all the values of a Dynamic Table

Using X-Path to Locate Web Table Elements

Before we locate web element, first let’s understands- What is a web element? Web elements are nothing but HTML elements like textbox, dropdowns radio buttons, submit buttons, etc. These HTML elements are written with start tag and ends with an end tag. For Example,

My First HTML Document

. Steps for getting X-path of web element that we want to locate. Step 1) In Chrome, Go to http://demo.guru99.com/test/web-table-element.php

Step 2) Right click on web element whose x-path is to be fetched. In our case, right click on “Company” Select Inspect option. The following screen will be shown –

Step 3) Right Click on highlighted web element > Select Copy -> Copy x-path option.

Step 4) Use the copied X-path “//*[@id=”leftcontainer”]/table/thead/tr/th [1]” in Selenium WebDriver to locate the element.

Example: Fetch number of rows and columns from Dynamic WebTable

While dynamic web table handling in Selenium, we cannot predict its number of rows and columns. Using Selenium web driver, we can find

Number of Rows and columns of web table in Selenium X row or Y column’s data.

Below is program for fetching total number of rows and columns for handling web table in Selenium:

Code Explanation:

Here we have first declared Web Driver object “wd” &initialized it to chrome driver. We use List to total number of columns in “col”. findElements commands returns a list of ALL the elements matching the specified locator using findElements and X-path //*[@id=\”leftcontainer\”]/table/thead/tr/th we get all the columns Similarly, we repeat the process for rows.

Output:

Example: Fetch cell value of a particular row and column of the Dynamic Table

Let’s assume we need 3rd row of the table and its second cell’s data. See the table below-

In above table, data is regularly updated after some span of time. The data you try retrieve will be different from the above screenshot. However, the code remains the same. Here is sample program to get the 3rd row and 2nd column’s data.

Table is located using locator property “tagname”. Using XPath “//[@id=\”leftcontainer\”]/table/tbody/tr[3]” find the 3rd row and gets its text using getText () function Using Xpath “//[@id=\”leftcontainer\”]/table/tbody/tr[3]/td[2]” find the 2nd cell in 3rd row and gets its text using getText () function

Output:

Example: Get Maximum of all the Values in a Column of Dynamic Table

In this example, we will get the maximum of all values in a particular column. Refer the following table –

Here is the code

Code Explanation:

Using chrome driver we locate the web table and get total number of row using XPath “.//*[@id=’leftcontainer’]/table/tbody/tr/td[1]” Using for loop, we iterate through total number of rows and fetch values one by one. To get next row we use (i+1) in XPath We compare old value with new value and maximum value is printed at the end of for loop

OutPut

Example: Get all the values of a Dynamic Table

Consider the following table http://demo.guru99.com/test/table.html

The number of columns for each row is different. Here row number 1, 2 and 4 have 3 cells, and row number 3 has 2 cells, and row number 5 has 1 cell. We need to get values of all the cells Here is the code:

Code Explanation:

rows_count gives the total number of rows for each row we get the total number of columns using rows_table.get(row).findElements(By.tagName(“td”)); We iterate through each column and of each row and fetch values.

Output:

Summary

By.xpath() is commonly used to access table elements. Static web tables in Selenium are consistent in nature. i.e. they do have fixed number of rows as well as Cell data. Dynamic web tables are inconsistent i.e. they do not have fixed number of rows and cells data. Using selenium web driver, we can handle dynamic web tables easily. Selenium Webdriver allows us to access dynamic web tables by their X-path