About this lesson
Iterate a web table in Selenium Java: loop over rows and read cells
Learn Selenium JavaAdvancedTables
How it works
To read a table you work in two levels: the rows, then the cells in each row. row.findElements(By.tagName("td")) searches inside that one row, so the returned list holds just its cells, and their positions match the column order in the header.
In the employee table, the cell at position 0 is the name, 1 the age, 2 the department and 3 the salary. Asking for position 4 throws IndexOutOfBoundsException, because each row has four cells.
Each findElements() is a call to the browser, so reading a large table cell by cell is slow. For big tables teams often return the whole table's text in one executeScript() call and parse it in Java.
About the assignment
The starter collects the rows of the employee table's tbody. You loop over the first three, read the name and department from each, and print them on one line per employee.
What you will do
- Loop over the first three rows of the list.
- In each row, collect the
tdcells withfindElements()called on the row. - Read the name cell and the department cell by their positions.
- Print the two values joined by a space, a hyphen and a space.
Common mistakes
Searching cells from the driver
driver.findElements(By.tagName("td"))returns every cell on the page, so the positions no longer belong to one row.Using the column number from the header labels
The department is the third column but sits at position 2, because lists start at 0.
Looping over every row
The task asks for the first three. Stop the loop at three, or the output has extra lines.
In a real test suite
Table loops turn a page into data a test can check: read every row into a list of records, then assert that a new record is present, that a sort order holds, or that totals add up.
Lesson 66 counted rows. Lesson 68 leaves tables for cookies, which are part of the browser session rather than the page.
Related lessons
- Count Table Rows and ColumnsCount the rows before looping over them.
- Get All Dropdown Options with getOptions()Loop over dropdown options, the same pattern on a different element.
- JavaScript Executor - Get Values
executeScript()return values, the basis for reading a whole table in one call.
To try one of these calls on any page without a task, open the Online Selenium Java Compiler.
Starter code
The editor above opens with this code. Fill in the TODO lines, then run and submit it.
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.List;
public class Main {
public static void main(String[] args) throws Exception {
WebDriver driver = new ChromeDriver();
driver.get("https://testkru.com/Elements/Tables");
WebElement table = driver.findElement(By.id("employeeTable"));
List<WebElement> rows = table.findElement(By.tagName("tbody")).findElements(By.tagName("tr"));
// TODO: Loop through first 3 rows
// TODO: Get name (column 0) and department (column 2) from each
// TODO: Print in format: "Name - Department"
driver.quit();
}
}