What is a Dynamic Web Table?
A dynamic web table is a table whose rows or columns are populated dynamically, often via JavaScript/AJAX. The number of rows/columns or their contents can change at runtime.
A dynamic web table is a table on a web page whose rows and columns are not fixed but change dynamically based on data retrieved from the server or user interaction. Unlike static tables (where the structure and data remain constant), dynamic web tables update in real-time or after user actions such as:
- Searching or filtering records.
- Paginating through results.
- Sorting columns.
- Loading data via AJAX calls or APIs.
Example: A list of orders in an e-commerce admin panel or employee details in an HR system where rows increase/decrease depending on backend data.
Challenges in Handling Dynamic Web Tables
- Changing number of rows and columns – You cannot rely on fixed positions.
- Dynamic IDs or locators – Elements may have auto-generated attributes.
- Pagination – Data might be spread across multiple pages.
- Sorting/Filtering – Data order may shift after user interaction.
- First, you locate the table as a whole (using <table>, <thead>, <tbody>, <tr>, <td>).
- Separate header (columns) from body (rows) to know where the actual data lies.
- Instead of hardcoding row/column numbers, use locators that fetch all available rows/columns at runtime.
- This ensures that even if the table expands or shrinks, your automation adapts.
- Loop through rows dynamically.
- For each row, capture all cells (columns) dynamically.
- This allows you to extract or validate data cell by cell.
- Navigate row by row, column by column, to locate a specific value (like “Employee Name = John”).
- Once found, perform actions such as clicking a related link, button, or checkbox in that row.
- If the table spans across multiple pages, Playwright should detect when the next page is available, navigate, and continue processing rows until the required data is found.
- Trigger sort/filter actions (e.g., click on column headers, apply filters).
- Re-extract table rows after the table reloads to ensure you are validating updated data.
- After locating data, verify correctness (e.g., salary column > 5000, status = Active).
- Assert based on table content to confirm that the dynamic loading is working as expected.
Handling a dynamic web table in Playwright with Java involves the following steps:
Steps to Handle Dynamic Table in Playwright Java
- Launch browser and go to the website
- Identify the table element
- Use locators to iterate through rows and columns
- Extract text or perform actions on cells
Example Website
We'll use this dummy site with a dynamic table:
https://demo.guru99.com/test/web-table-element.php
Maven Dependency
<dependencies>
<dependency>
<groupId>com.microsoft.playwright</groupId>
<artifactId>playwright</artifactId>
<version>1.44.0</version>
</dependency>
</dependencies>
import com.microsoft.playwright.*; public class DynamicWebTableExample { public static void main(String[] args) { try (Playwright playwright = Playwright.create()) { Browser browser = playwright.chromium().launch(new BrowserType.LaunchOptions().setHeadless(false)); Page page = browser.newPage(); // Navigate to the sample dynamic table page page.navigate("https://demo.guru99.com/test/web-table-element.php"); // Locate the table Locator table = page.locator("table.dataTable"); // Get number of rows (excluding the header row) int rowCount = table.locator("tbody > tr").count(); System.out.println("Total Rows: " + rowCount); // Get number of columns (assuming all rows have same number) int colCount = table.locator("thead > tr > th").count(); System.out.println("Total Columns: " + colCount); // Print table contents for (int i = 0; i < rowCount; i++) { for (int j = 0; j < colCount; j++) { String cellText = table.locator("tbody > tr").nth(i).locator("td").nth(j).innerText(); System.out.print(cellText + " | "); } System.out.println(); } browser.close(); } } }
Code explanation
- table.dataTable – CSS selector for the web table.
- tbody > tr – All rows in the table body.
- thead > tr > th – All header columns.
- We loop over each row and column to print content.
Suggested Posts:
1. BrowserContext in Playwright
2. File Upload in Playwright
3. Comma Selectors in Playwright
4. Thread Local in Playwright
5. Handle GET API in Playwright