Skip to content
CodeKruPro

Count the rows and columns of a web table

Automation taskEasy-MediumAsked in: Accenture, Infosys, TCS, Wipro

The usual web table opener: "How many rows and columns does this table have?" Count the rows in the table body (the data rows, in tbody) and the header cells (in thead). Leave the header row out of the row count.

Count Table Rows and Columns

Count Table Rows and Columns

Count rows using <tr> in <tbody> and columns using <th> or <td> in the first row.

Table Structure (thead vs tbody):

<table id="employeeTable">
  <thead>                      <!-- Header section -->
    <tr>
      <th>Name</th><th>Age</th><th>Department</th><th>Salary</th>
    </tr>
  </thead>
  <tbody>                      <!-- Data section (count rows here) -->
    <tr><td>John Smith</td><td>30</td><td>Engineering</td><td>$80,000</td></tr>
    <tr><td>Jane Doe</td><td>25</td><td>Marketing</td><td>$65,000</td></tr>
    <!-- ... 5 rows total ... -->
  </tbody>
</table>

Task:

  1. Navigate to https://testkru.com/Elements/Tables
  2. Count the number of data rows (in tbody) in the employeeTable
  3. Count the number of columns (headers)
  4. Print both counts

Expected output:

Data rows: 5
Columns: 4