Testleaf

Managing Web Tables in Selenium: Dynamic Rows, Columns & Filters Like a Pro

Managing Web Tables in Selenium Dynamic Rows, Columns & Filters Like a Pro

 

Web tables are everywhere—from dashboards and reports to admin panels and user listings. But for automation testers, web tables can be a total headache—especially when dealing with dynamic content, unpredictable row counts, or tricky filters. 

In this guide, we’re going to break down exactly how to handle dynamic web tables using Selenium, including how to deal with filters, capture cell data, and validate rows and columns effectively. Whether you’re a beginner or looking to sharpen your skills, this one’s for you. 

 

 What Makes Web Tables Tricky? 

Before we dive into solutions, let’s get real—web tables are not always as clean as we want them to be. Here’s why: 

  • Rows and columns are generated dynamically based on data 
  • IDs or classes for table elements are either missing or not reliable 
  • Filters change the structure of the table 
  • Pagination adds complexity 
  • Tables are sometimes loaded via AJAX (hello, synchronization issues)

What Makes Web Tables Tricky

If you try to use static XPath or rely on fixed row/column counts, your tests are bound to break. So let’s look at how to automate like a pro. 

Other Helpful Articles: selenium interview questions

 

Understanding the Table Structure 

First, inspect the DOM using Chrome DevTools (Right-click > Inspect). Check how the table is built: 

<table id=”userTable”>
<thead>
<tr>
<th>Name</th>
<th>Email</th>
<th>Status</th>
</tr>
</thead>
<tbody>
<tr>
<td>John Doe</td>
<td>john@example.com</td>
<td>Active</td>
</tr>
<!– more rows –>
</tbody>
</table>

Here’s how to locate rows and cells dynamically: 
Total Row Count: 

List<WebElement> allRows = driver.findElements (By.xpath(“//table[@id=’userTable’]/tbody/tr”));
System.out.println(“Total rows: + allRows.size());

 Total Column Count (first row): 

List<WebElement> allColumns = driver.findElements (By.xpath(“//table[@id=’userTable’]/tbody/tr[1]/td”));
System.out.println (“Total columns: + allcolumns.size());

Playwright automation testing

 

Looping Through All Table Data 

You can loop through all the rows and columns dynamically like this: 

List<WebElement> allRowS = driver.findElements (By.xpath(“//table[@id=’userTable’]/tbody/tr”));
for (int i = 1; i < allRows.size(); i++) {
List<WebElement> cells = driver.findElements (By.xpath(“//table[@id=’userTable’]/tbody/tr[” + i + “]/td”));
for (WebElement cell cells) {
System.out.print(cell.getText() + “);
System.out.println();
}
}

This will print the full table content row by row. Clean and simple! 

You Should Also Read: automation testing interview questions

 

Searching for Specific Cell Data 

Let’s say you want to find the row where the user’s name is “John Doe” and verify their status is “Active”. 

for (int i=1; i < allRows.size(); i++) {
String name = driver.findElement(By.xpath(“//table[@id=’userTable’]/tbody/tr[“1″]/td[1]”)).getText();
if (name.equals(“John Doe”)) {
String status driver.findElement(By.xpath(“//table[@id=”userTable’]/tbody/tr[“1″]/td[3]”)).getText();
System.out.println(“Status: ” + status);
break;
}
}

 

 

Handling Filters in Tables 

Tables with filters might reload using AJAX when you search or apply filters. Here’s how to deal with it: 

1. Apply filter via UI: 

driver.findElement(By.id(“searchBox”)).sendKeys(“John

2. Wait for table to refresh: 

WebDriverwait wait = new WebDriverwait(driver, Duration.ofseconds(10));
wait.until(ExpectedConditions.textToBePresentInElementLocated(
By.xpath(“//table[@id=’userTable’]/tbody/tr[1]/td[1]”), “John Doe”));

Selenium training in chennai
3. Validate result: 

String filteredName = driver.findElement(By.xpath(“//table[@id=’userTable*]/tbody/tr[1]/td[1]”)).getText();
Assert.assertEquals(filteredName, “John Doe”);

 

Example Scenario: Validate Email of Active Users Only 

List<WebElement> rows= driver.findElements (By.xpath(“//table[@id=’userTable’]/tbody/tr”));
for (int i=1; i <= rows.size(); i++) {
}
String status driver.findElement(By.xpath(“//table[@id=’userTable”]/tbody/tr[” + i + “]/td[3]”)).getText();
if (status.equalsIgnoreCase(“Active”)) {
}
String email driver.findElement(By.xpath(“//table[@id=’userTable’]/tbody/tr[” + i + “]/td[2]”)).getText();
System.out.println(“Active user email: + email);

 

Dive Deeper: api automation interview questions

Tips for Cleaner Code 

  • Use String.format() to create dynamic XPath templates 
  • Avoid hardcoding table structures — always go dynamic 
  • Use reusable methods to get row/column/cell data 

Tips for Cleaner Code

Advanced: Handling Pagination 

Some web tables paginate, showing only 10–20 rows per page. You need to click through pages and loop through data: 

boolean nextPageExists = true;
while (nextPageExists) {
// handle current page
List<WebElement> rows = driver.findElements (By.xpath(“//table[@id=’userTable’]/tbody/tr”));
for (WebElement row rows) {
System.out.println(row.getText());
}
// try clicking ‘Next’
try {
WebElement nextBtn = driver.findElement(By.id(“nextPageBtn”));
if (nextBtn.isDisplayed()) {
nextBtn.click();
Thread.sleep(2000); // or use WebDriverwait
} else {
nextPageExists = false;
}
} catch (NoSuchElementException e) {
nextPageExists = false;
}
}

 Final Thoughts 

Working with web tables in Selenium might look intimidating at first, but once you understand how to build dynamic XPath, loop through rows/columns, and handle filters or pagination, it becomes a breeze. 

Keep your code modular and reusable. Abstract away repetitive logic into helper methods and Page Object Models. And always verify your XPath using browser dev tools before using it in code. 

If you are looking to sharpen these skills with hands-on guidance, consider enrolling in selenium training in Chennai, where real-world projects make concepts easier to master.

FAQs

1. How do you handle dynamic web tables in Selenium?
By using dynamic XPath, looping through rows and columns, and applying reusable methods instead of relying on fixed row or column counts.

2. How to fetch specific cell data in Selenium web tables?
Inspect the DOM, build dynamic XPath, and loop through rows until the target cell is found. You can also filter by unique text like a username or email.

3. How can you apply filters in Selenium web tables?
Apply filters via the UI, wait for the table to refresh (handle AJAX if used), and then validate the results dynamically.

4. How do you handle pagination in Selenium web tables?
Click through each page programmatically, loop through all rows, and capture data across multiple pages until the end of the table is reached.

5. What are best practices for automating web tables in Selenium?
Use the Page Object Model, build dynamic XPath with String.format(), avoid hardcoding, and modularize your code with reusable helper methods.

We Also Provide Training In:
Author’s Bio:

Dilip

As a Senior SDET with 8+ years in testing and development, I build scalable automation platforms ensuring quality at speed. Passionate about mentoring and innovation, I equip teams with real-time solutions and high-impact frameworks, driving excellence through continuous learning. Let’s shape the future of quality engineering together.

Dilipkumar Rajendran
Senior SDET | Playwright & Selenium Expert

                                                                         LinkedIn Logo

 

Accelerate Your Salary with Expert-Level Selenium Training

X