Skip to content
CodeKruPro

Read the header cells of a table

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

"Print the column names of this table." Find all header cells inside the table and read them in order. Once you know which position each name has, later code can read columns by name instead of by number.

Read Table Headers with findElements()

Read Table Headers

HTML tables use <th> for header cells. Use findElements() to get all headers.

On the Tables page, the employee table (id="employeeTable") has headers: Name, Age, Department, Salary.

HTML Table Structure:

<table id="employeeTable">
  <thead>
    <tr>
      <th>Name</th>
      <th>Age</th>
      <th>Department</th>
      <th>Salary</th>
    </tr>
  </thead>
  <tbody>
    <tr><td>...</td><td>...</td><td>...</td><td>...</td></tr>
    <!-- 5 data rows -->
  </tbody>
</table>

Task:

  1. Navigate to https://testkru.com/Elements/Tables
  2. Find the employeeTable
  3. Get all <th> elements
  4. Print each header text

Expected output:

Header 1: Name
Header 2: Age
Header 3: Department
Header 4: Salary