Skip to content
CodeKruPro

Read every row of a table

Automation taskMediumAsked in: Accenture, Infosys, TCS, Wipro

Asked as: "Print every employee with their department", or "find the row with a given value". Loop over the body rows and read the cells of each row. Searching for cells inside the row keeps the code short.

Iterate All Table Data

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