Table of contents
1.
Introduction
2.
What is a Web Table?
3.
Types of Web Tables
4.
How to handle web tables in Selenium?
4.1.
Finding XPath Selected Element in Dynamic Web Table
4.2.
Finding the number of rows and columns 
4.3.
Finding cell values for specific row & column 
5.
Frequently Asked Questions
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

Handling Dynamic Web Tables

Author Parth Jain
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Selenium is one of the most popular automation testing tools used to ensure that a website works as expected. Furthermore, Selenium ensures that a website aligns with predetermined technical and business requirements. Selenium aids the website developers in complying with the ever-growing demands made upon them.
Selenium Webdriver is an essential component of the Selenium Test Suite, a web framework that runs automated tests on websites to ensure all UI elements are functioning accurately.
Among these UI elements, web tables are essential for a website in which information is required in a tabular format. These tables can be both static and dynamic in nature.
This blog will help you understand Selenium's web tables and how to handle them.

What is a Web Table?

A web Table is similar to a standard table. Here the data is represented in a structured form using rows and columns. The critical difference between a table and a web table is that a web table is displayed on the web with the assistance of HTML code.
<table> is an HTML tag that is used to define a web table. Furthermore,the <th> tag is used to define the header of a table.Similarly, <tr> and <td> tags are used to define rows and columns respectively for a web table.
Consider the example below:

<table>
 <tr>
  <th>Ninja Name</th>
  <th>Ninja Email</th>
  <th>Ninja Age</th>
 </tr>
 <tr>
  <td>Rohan</td>
  <td>rohan@codingninjas.com</td>
  <td>17</td>
 </tr>
 <tr>
  <td>Tarush</td>
  <td>tarush@codingninjas.com</td>
  <td>21</td>
 </tr>
</table>

Web Table 

Types of Web Tables

  • Static Web Tables: The Static web tables have fixed data that cannot be changed throughout. Due to this static nature, they are called Static tables.
  • Dynamic Web Tables: The Dynamic web tables have data that can change over time, and hence the number of rows and columns can also vary depending upon the data. Due to this dynamic nature, they are called Dynamic tables.

How to handle web tables in Selenium?

Let us get started with the various operations that can be performed on a Selenium Web Table.

Finding XPath Selected Element in Dynamic Web Table

Let's start by selecting an element in the web table and finding its XPath.To locate the XPath of a UI element with the help of a browser, right-click on the desired element, and click on the "Inspect Element" to open the inspector option, which will help locate an element XPath.

Finding the number of rows and columns 

//To Find the number of Rows
List<WebElement> rowsNumber = driver.findElements(By.xpath(“//*[@id="content-8b4988b9-2ec9-4e77-9b4d-9c2994bd9e8a"]/div/div/table[1]/tbody/tr[1]/td[1]”));
int row = rowsNumber.size();
System.out.println("The No. of rows in this table is : " + row);

Output: The No. of rows in this table is: 8

//To Find the number of Columns
List<WebElement> columnsNumber = driver.findElements(By.xpath("//*[@id="content-8b4988b9-2ec9-4e77-9b4d-9c2994bd9e8a"]/div/div/table[1]/thead/tr/th[1]"));
int column = columnsNumber.size();
System.out.println("The No. of columns in this table is : " + column);

Output: The No. of columns in this table is: 9

Finding cell values for specific row & column 

WebElement cellAddress = Rowtable.findElement(By.xpath(“/html/body/table/tbody/tr[2]/td[2]”));
String value = cellAddress.getText();
System.out.println(“The Cell Value is : “ +value);

Output:The Cell Value is: rohan@codingninjas.com

Check this out : Xpath in Selenium

Frequently Asked Questions

  1. What are Static Web Tables?
    The Static web tables have fixed data that cannot be changed throughout. Due to this static nature, they are called Static tables.
  2. What are Dynamic Web Tables?
    The Dynamic web tables have data that can change over time, and hence the number of rows and columns can also vary depending upon the data. Due to this dynamic nature, they are called Dynamic tables.
  3. How to find the X-Path of an element in a table?
    To find x-path, click right and navigate over to the Inspect Element option. Then navigate the elements HTML inside the developer options and click right to copy the X-path.
  4. What are Web Tables?
    A web Table is similar to a standard table. Here the data is represented in a structured form using rows and columns. The critical difference between a table and a web table is that a web table is displayed on the web with the assistance of HTML code.
  5. What are the various tags used in a web table?
    <table> is an HTML tag that is used to define a web table. Furthermore, the <th> tag is used to define the header of a table.Similarly, <tr> and <td> tags are used to define rows and columns respectively for a web table.

Conclusion

In this article, we have extensively discussed Dynamic Web Tables in Selenium and how to handle them. If you are Preparing for interview and don't know where to start, we have got you covered, check out our expert curated courses on our website, You can also check out Coding Ninjas Studio to practice frequently asked interview problems. We hope that this blog has helped you enhance your knowledge regarding web tables and if you would like to learn more, check out our articles.  Do upvote our blog to help other ninjas grow. Happy Coding!"

Live masterclass