Skip to content
CodeKruPro

Iterate All Table Data

67/70

Iterate All Table Data

Loop through all rows and cells to extract complete table data.

Row/Cell Structure:

<tbody>
  <tr>
    <td>John Smith</td>     <!-- cells.get(0) = Name -->
    <td>30</td>              <!-- cells.get(1) = Age -->
    <td>Engineering</td>     <!-- cells.get(2) = Department -->
    <td>$80,000</td>        <!-- cells.get(3) = Salary -->
  </tr>
  <tr>
    <td>Jane Doe</td>
    <td>25</td>
    <td>Marketing</td>
    <td>$65,000</td>
  </tr>
  <!-- ... more rows ... -->
</tbody>

Task:

  1. Navigate to https://testkru.com/Elements/Tables
  2. Get the first 3 employee names from the employeeTable
  3. For each, print the name and their department

Expected output:

John Smith - Engineering
Jane Doe - Marketing
Bob Johnson - Sales

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

  1. Loop over the first three rows of the list.
  2. In each row, collect the td cells with findElements() called on the row.
  3. Read the name cell and the department cell by their positions.
  4. 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.

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.

Main.java
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();
    }
}