Skip to content
CodeKruPro

Count Table Rows and Columns

66/70

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

About this lesson

Count table rows and columns in Selenium Java

Learn Selenium JavaAdvancedTables

How it works

Counting is findElements() plus size(). The question is what to count. Data rows are the tr elements inside tbody; columns are the th cells of the header row, or the td cells of any one data row.

Where you search changes the answer. On the employee table, tr elements under the whole table include the header row, so that count is one higher than the number of data rows. Searching tr across the whole page also picks up the second table's rows.

A CSS selector can do the scoping in one step: #employeeTable tbody tr matches only the data rows of this table.

About the assignment

The starter finds the employee table. You count its data rows and its columns and print both counts, choosing the elements so that the header row is not counted as data.

What you will do

  1. Find the table's tbody, then call findElements() on it for tr elements, and print the list's size after the label Data rows:.
  2. Count the table's th cells the same way and print the size after the label Columns:.

Common mistakes

  • Counting tr under the table

    The header row is a tr too, so the data row count comes out one too high.

  • Counting from the driver

    Rows and headers from dynamicTable are added in, so both counts are too high.

  • Counting td across the whole table for columns

    That gives rows times columns, not the number of columns. Count the cells of one row.

In a real test suite

Row counts check search results, pagination ("25 per page") and filters ("only Engineering rows"). After adding or deleting a record, a test often compares the count before and after.

Lesson 65 read single cells. Lesson 67 loops over the rows and reads cells from each one.

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"));
        
        // TODO: Count rows in tbody
        // TODO: Count columns (th elements)
        // TODO: Print both
        
        driver.quit();
    }
}