Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Java Arrays
  • Java Strings
  • Java OOPs
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Java MCQs
  • Spring
  • Spring MVC
  • Spring Boot
  • Hibernate
Open In App
Next Article:
Selenium WebDriver-Handling Alerts
Next article icon

A Guide to Handling a WebTable in Selenium Webdriver

Last Updated : 30 Sep, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

A Dynamic web table is a type of web table where the number of rows and columns are not constant, they keep changing from time to time depending on the requirements or based on the query, i.e. Number of rows and columns is NOT fixed.

What is a WebTable in Selenium?

A WebTable in Selenium refers to an HTML table that contains structured data, typically organized in rows and columns. Selenium allows users to interact with these tables by locating elements based on their attributes, such as tags, classes, or IDs, enabling actions like extracting data, clicking on links within the table, or validating content. Handling WebTables often involves using methods to traverse rows and cells, making it crucial for testing applications that rely on tabular data.

What are the Two Main Types of WebTables in Selenium?

In Selenium, the two main types of WebTables are:

Static WebTable

Static WebTables have a fixed structure and content that doesn't change after the page loads. The data is predefined, allowing for straightforward extraction and interaction since the number of rows and columns remains constant. Testing involves simply locating elements and retrieving values without concern for changes in the table’s structure.

Dynamic WebTable

Dynamic WebTables, on the other hand, can change based on user interactions or data updates, such as sorting, filtering, or pagination. This type requires more complex handling because the number of rows, columns, or even the data within them can vary. Selenium scripts need to account for these changes, often involving methods to wait for elements to load or to interact with controls that modify the table's content.

Handling WebTables in Selenium (For both static and Dynamic)

Handling Dynamic Table in Selenium

The below table is a dynamic web table and the HTML code for the table, This table does not contain even an arrangement of rows and columns the last row has two columns, but the other has 4 columns. 

HTML
<html>    <head>       <style></style>    </head>    <body>       <table name="Table">          <tr>             <th>BookName</th>             <th>Author</th>             <th>Subject</th>             <th>Price</th>          </tr>          <tr>             <td>Learn Selenium</td>             <td>John</td>             <td>Selenium</td>             <td>100</td>          </tr>          <tr>             <td>Learn Java</td>             <td>Joey</td>             <td>Java</td>             <td>500</td>          </tr>          <tr>             <td>Learn JS</td>             <td>Chandler</td>             <td>Javascript</td>             <td>700</td>          </tr>          <tr>             <td>Master In Selenium</td>             <td>Ross</td>             <td>Selenium</td>             <td>1000</td>          </tr>          <tr>             <td>Master In Java</td>             <td>Mike</td>             <td>JAVA</td>             <td>2000</td>          </tr>          <tr>             <td>Master In JS</td>             <td>Rachel</td>          </tr>       </table>    </body> </html> 

Save the code as “.html”, then you will get an HTML table like below.

Example for fetch no. of rows and columns from Dynamic Table

The main problem that occurs during working with the Dynamic table is, that we cannot predict the number of rows and columns. So in this example, we will use the Selenium web driver to find the number of rows and columns. For computing the number of rows and columns, we require the Xpath of the web table.

Find the X-Path of the Table:

Go to the website, Right-click on the table, and select inspect and copy the x-path.

X-Path for Columns:

/html/body/table/tbody/tr[1]/th

X-Path for Rows:

/html/body/table/tbody/tr/td[1]

Program

Java
import java.util.List; import org.openqa.selenium.By; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.testng.annotations.Test;  public class Geeks {      String columnXpath = "/html/body/table/tbody/tr[1]/th";     String rowXpath = "/html/body/table/tbody/tr/td[1]";     @Test public void geeksforgeeks()     {         // Please note that with Selenium 4.6.0 version, a         // new feature is added called Selenium Manager With         // Selenium Manager there is no need to use any         // driver, rather Selenium can handle itself.         System.setProperty(             "webdriver.chrome.driver",             "C:\\Users\\ADMIN\\Documents\\chromedriver.exe");         ChromeDriver driver = new ChromeDriver();          // Maximize the browser         driver.manage().window().maximize();          // Launch Website         driver.get(             "file:///C:/Users/ADMIN/Desktop/table.html");          // Number of columns         List<WebElement> col             = driver.findElements(By.xpath(columnXpath));         System.out.println("No of columns : " + col.size());          // Number of rows         List<WebElement> rows             = driver.findElements(By.xpath(rowXpath));         System.out.println("No of rows : " + rows.size());         driver.close();     } } 

Code Explanation:

We declared the selenium web driver object "driver" initialized it to chrome driver, and used the 'List<webelement>' list of web element datatype to find the number of columns and rows.

To set up the chrome driver using selenium refer to this article How to open chrome browser using Selenium in Java

Output:

Output

Conclusion

Handling WebTables in Selenium WebDriver is a fundamental skill for automating web applications that present data in tabular format. Understanding the differences between static and dynamic tables allows testers to apply appropriate strategies for data extraction, interaction, and validation.

  • For static WebTables, the simplicity of their fixed structure enables straightforward element identification and data manipulation. In contrast, dynamic WebTables require a more nuanced approach due to their changing nature, necessitating the use of explicit waits and careful navigation through pagination or filtering.

Implementing best practices, such as modular code and robust error handling, enhances the reliability and maintainability of your test scripts. By mastering these techniques, you can effectively automate interactions with WebTables, ensuring comprehensive testing of applications that rely on structured data. This not only improves efficiency in testing but also contributes to delivering high-quality software that meets user expectations.


Next Article
Selenium WebDriver-Handling Alerts

A

allwink45
Improve
Article Tags :
  • Java
  • Software Testing
  • Selenium
  • selenium
Practice Tags :
  • Java

Similar Reads

  • Selenium WebDriver-Handling Alerts
    Selenium is one of the most popular and powerful tools for automated web applications. Selenium is widely used for automating user interactions like filling out forms, navigating to a website clicking on a button, etc. While we are dealing with automating user interactions one of the most common sce
    5 min read
  • Selenium WebDriver - WebElement Commands
    Selenium is an open-source program that automates web browsers. Selenium Webdriver is mainly used to execute the scripts according to the browser we are using. Prerequisites1. Install the Selenium Python Package.Use the following command to install the Selenium Python package: pip install selenium 2
    6 min read
  • How to check if an element exists with Selenium WebDriver?
    Selenium is one of the most popular tools for automating web applications. Selenium is not a single tool but rather a set of tools that helps QA testers and developers to automate web applications. It is widely used to automate user interactions on a web page like filling out web forms, clicking on
    7 min read
  • Selenium WebDriver - Navigation Commands
    Selenium WebDriver is quite a popular open-source framework used for automating web browsers. It helps the developers to automate web-based operations and interactions by creating code in Java, Python, C#, and others. When automating tests, Selenium WebDriver offers a set of navigation commands that
    6 min read
  • Wait Until Page Is Loaded With Selenium WebDriver For Python
    Selenium is an automation tool or a web framework used mainly in testing web applications across various browsers. Apart from testing web applications, we can also perform various tasks with selenium. With the help of selenium, we can also perform various web related tasks such as web scraping, web
    5 min read
  • How to Automate Click Using Selenium WebDriver?
    Selenium is one of the most popular and powerful tools for automating web applications. Selenium is widely used for automating user interactions like filling out forms, clicking on a button, navigating to a web page, and many more. One of the most common tasks while working with Selenium is clicking
    6 min read
  • In Selenium WebDriver, use Python to obtain the WebElement's HTML source?
    The article focuses on discussing how to use Python to obtain the WebElement's HTML source. Prerequisites: Selenium Python Installation: Using Selenium Webdriver, one can obtain the HTML source of any WebElement. What is an HTML Source? HTML Source is described as the HTML code that underlies a cert
    2 min read
  • Applications and Uses of Selenium WebDriver
    Selenium Webdriver is a powerful tool for controlling web browser through program. It is functional for all browsers, works on all major OS and its scripts are written in various languages i.e Python, Java, C#, etc. Selenium Webdriver is a primary automation tool used by developers all around the wo
    3 min read
  • How to Drag and Drop an Element using Selenium WebDriver in Java?
    Selenium is an open-source web automation tool that supports many user actions to perform in the web browser. Automating a modern web page that has a drag and drop functionality and drag and drop is used to upload the files and so many user activities. so to perform the drag and drop actions the sel
    2 min read
  • Introduction to Selenium WebDriver - GeeksforGeeks
    Selenium WebDriver is a powerful Automation tool widely used for web application testing. It provides a programming interface to interact with web browsers, allowing users to automate browser actions, navigate web pages, and perform functional testing. With support for multiple programming languages
    8 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences