How to operate with WebTable
What is a WebTable?
A WebTable is an HTML table (<table>) containing rows (<tr>) and cells (<td>), commonly seen in data grids, reports, or search results.
Basic Approach
List<WebElement> rows = driver.findElements(By.xpath("//table/tbody/tr"));
for (WebElement row : rows) {
List<WebElement> cells = row.findElements(By.tagName("td"));
System.out.println(cells.get(0).getText());
}
Why This Matters
Iterating through rows and cells lets a test verify specific data values, count matching rows, or locate a row based on its content (like finding the row for a specific username).
Reading a Specific Cell and Finding a Row by Its Content
Looping over every row is a start, but most real tests need something more targeted: read one cell, or act on the row that belongs to a particular record. XPath handles both neatly.
// Value in row 2, column 3 (XPath positions start at 1, not 0)
String city = driver.findElement(
By.xpath("//table[@id='users']/tbody/tr[2]/td[3]")).getText();
// The row whose first cell is exactly "rahul.k", then click its Edit button
WebElement row = driver.findElement(
By.xpath("//table[@id='users']/tbody/tr[td[1][normalize-space()='rahul.k']]"));
row.findElement(By.xpath(".//button[normalize-space()='Edit']")).click();
// Count rows that show the status "Active"
int active = driver.findElements(
By.xpath("//table[@id='users']/tbody/tr[td[normalize-space()='Active']]")).size();
The second locator says "find a tr that contains a first td with this text". Then the search continues inside that row, using .// so it stays within the row instead of searching the whole page. Using normalize-space() ignores extra spaces and line breaks around the text, which often break exact matches.
Mapping Column Headers Instead of Hard-Coding Indexes
If developers add or reorder a column, tests that rely on fixed positions break or, worse, read the wrong data and still pass. Reading the header row once and building a name-to-index map makes tests far more robust.
new WebDriverWait(driver, Duration.ofSeconds(10))
.until(ExpectedConditions.visibilityOfElementLocated(
By.cssSelector("#users tbody tr")));
List<WebElement> headers = driver.findElements(By.cssSelector("#users thead th"));
Map<String, Integer> col = new HashMap<>();
for (int i = 0; i < headers.size(); i++) {
col.put(headers.get(i).getText().trim(), i);
}
for (WebElement r : driver.findElements(By.cssSelector("#users tbody tr"))) {
List<WebElement> cells = r.findElements(By.tagName("td"));
String user = cells.get(col.get("Username")).getText();
String status = cells.get(col.get("Status")).getText();
System.out.println(user + " -> " + status);
}
The explicit wait at the top matters because many tables load their rows through an API call after the page appears. Without it, the test may count zero rows simply because it looked too early.
Handling Tricky Tables and Avoiding Common Errors
- Header cells are not td. Headings usually use
th, sotdlists skip them. Some tables also put the header insidetbody, which shifts your row numbers by one. - Stale elements after sorting or paging. Clicking a sort header or the next page rebuilds the rows, and saved
WebElementreferences throwStaleElementReferenceException. Find the rows again after every such action. - Pagination. To search across pages, loop: check the current page, and if the record is not there, click Next until it is found or the Next button is disabled.
- Tables built from div elements. Modern grids often use
divtags with roles such asrole="row"androle="gridcell". The same techniques work, with locators based on those roles. - Virtual scrolling. Some grids render only visible rows. Scrolling may be required before a row exists in the page at all.
WebTable checklist
- Wait for the rows, not just the table element, before reading data.
- Locate rows by unique content such as an ID or username, not by position.
- Resolve column positions from header names.
- Re-locate elements after sorting, filtering or paging.
- Move table logic into a page object method, such as
getStatusFor(username), so tests stay short and readable.
Ready to master real-world software testing?
Learn manual and automation testing hands-on with mentor-led sessions.