Selenium Web Tables
Web tables display data in rows and columns: orders, students, prices, or bank entries. A test often needs one cell from the middle of the grid, such as the price of the third product. Think of a cinema seating chart. You find a seat by row letter and column number. Selenium reads a table with the same two coordinates.
Anatomy of an HTML Table
<table>
<thead>
<tr> <th>Name</th> <th>City</th> <th>Score</th> </tr>
</thead>
<tbody>
<tr> <td>Asha</td> <td>Delhi</td> <td>88</td> </tr>
<tr> <td>Ravi</td> <td>Pune</td> <td>92</td> </tr>
</tbody>
</table>
| Tag | Meaning |
|---|---|
| table | The whole grid |
| thead / tbody | Header section and body section |
| tr | One row |
| th | Header cell |
| td | Data cell |
Coordinate Picture
col1 col2 col3
+-------+-------+-------+
row1 | Asha | Delhi | 88 |
+-------+-------+-------+
row2 | Ravi | Pune | 92 |
+-------+-------+-------+
row3 | Meena | Goa | 79 |
+-------+-------+-------+
Cell (row3, col3) = //tbody/tr[3]/td[3] -> 79
Reading One Cell
String score = driver.findElement(
By.xpath("//table[@id='results']/tbody/tr[3]/td[3]")).getText();
System.out.println(score);Counting Rows and Columns
List<WebElement> rows = driver.findElements(By.xpath("//table[@id='results']/tbody/tr"));
List<WebElement> cols = driver.findElements(By.xpath("//table[@id='results']/thead/tr/th"));
System.out.println("Rows: " + rows.size() + " Columns: " + cols.size());Reading the Whole Table
A nested loop visits every row and every cell. The outer loop walks the rows, and the inner loop walks the cells of the current row.
for (WebElement row : rows) {
List<WebElement> cells = row.findElements(By.tagName("td"));
for (WebElement cell : cells) {
System.out.print(cell.getText() + " | ");
}
System.out.println();
}Searching by Content
Row positions change when data changes. A content-based locator stays steady. The following XPath finds the row that contains Ravi and reads the city from the same row.
String city = driver.findElement(
By.xpath("//td[text()='Ravi']/following-sibling::td[1]")).getText();
Ravi ---> following-sibling::td[1] ---> Pune
Clicking a Button Inside a Row
Many tables hold an Edit or Delete button in each row. Anchor on the name and travel to the button inside the same row.
driver.findElement(By.xpath(
"//td[text()='Meena']/parent::tr//button[text()='Delete']")).click();Finding the Highest Value
int max = 0;
for (WebElement row : rows) {
int value = Integer.parseInt(row.findElement(By.xpath("./td[3]")).getText());
if (value > max) { max = value; }
}
System.out.println("Highest score: " + max);Pagination and Sorting
Large tables split data across pages. Read the current page, click the Next button, and repeat until the button becomes disabled. Sorting tests click a header and compare the column text against a sorted copy of the same list.
Read page 1 -> click Next -> read page 2 -> ... -> Next disabled -> stop
Key Points
- Use row and column numbers for fixed tables.
- Use sibling and parent axes for changing tables.
- Loop through rows with findElements.
- Wait for table content before reading it.
Static Tables and Dynamic Tables
A static table holds the same rows on every load. Fixed coordinates work well. A dynamic table receives rows from a database. Rows appear, vanish, and reorder as users add records. Dynamic tables demand content-based locators. The third row today may hold a different person tomorrow.
Building a Helper Method
A reusable method keeps tests short and readable. The method accepts a row key and a column name, locates the matching cell, and returns its text. Every test calls the helper instead of repeating long XPath strings.
public String getCell(String rowKey, int columnNumber) {
String xpath = "//table[@id='results']//td[text()='" + rowKey
+ "']/parent::tr/td[" + columnNumber + "]";
return driver.findElement(By.xpath(xpath)).getText();
}
String city = getCell("Ravi", 2);Verifying Table Data in a Test
Good table tests confirm facts a business cares about. Check that a new order appears after checkout. Check that a deleted record disappears. Check that the total row equals the sum of the item rows. Compare the table text with the expected value using an assertion, and include the row key in the failure message. A reader knows which record failed.
Handling Tables Built from Div Tags
Modern frameworks often draw tables with div elements that carry role attributes instead of table tags. The same ideas apply. Locate the container, collect the child rows, and read the child cells. Replace tr and td in the XPath with the role or class names the page uses, such as //div[@role='row'] and .//div[@role='cell'].
Speed Tips
Each findElement call costs one round trip to the browser. A loop that calls it hundreds of times slows a test. Fetch all rows once, and read text from the stored list. When the table holds thousands of rows, test a filtered view rather than the full dataset.
