Skip to content
CodeKruPro

Get a Table Cell Value by Its id

65/70

Get Specific Table Cell Value

You can navigate to a specific cell using row and column indices or by element IDs.

The employee table has cells with IDs like emp1Name, emp3Dept, etc.

Table Cell ID Pattern:

<table id="employeeTable">
  <tbody>
    <tr>
      <td id="emp1Name">John Smith</td>
      <td id="emp1Age">30</td>
      <td id="emp1Dept">Engineering</td>
      <td id="emp1Salary">$80,000</td>
    </tr>
    <tr>
      <td id="emp2Name">Jane Doe</td>
      <td id="emp2Age">25</td>
      <td id="emp2Dept">Marketing</td>
      <td id="emp2Salary">$65,000</td>
    </tr>
    <!-- Pattern: emp{N}Name, emp{N}Age, emp{N}Dept, emp{N}Salary -->
  </tbody>
</table>

Task:

  1. Navigate to https://testkru.com/Elements/Tables
  2. Get the name of employee 1 (John Smith)
  3. Get the department of employee 3 (Sales)
  4. Get the salary of employee 4 ($85,000)

Expected output:

Employee 1 Name: John Smith
Employee 3 Dept: Sales
Employee 4 Salary: $85,000

About this lesson

Get a table cell value in Selenium Java by the cell's id

Learn Selenium JavaAdvancedTables

How it works

When cells have ids, reading one is a plain driver.findElement(By.id("emp2Age")) followed by getText(). The employee table on the Tables page gives every cell an id built from the row number and the column: emp2Name, emp5Dept, emp1Salary. That pattern lets a test compute the id it needs instead of walking the table.

getText() returns the cell exactly as displayed, with formatting. Salaries come back with the dollar sign and the thousands comma, so they are text, not numbers. Strip the symbols before converting if you need arithmetic.

Without ids, the same cell is reached by position, for example By.xpath("//table[@id='employeeTable']/tbody/tr[2]/td[1]") for the second row's name. Position-based locators break when rows are sorted or columns move, which is why ids are preferred when the app provides them.

About the assignment

You read three cells from the employee table using only their ids: one employee's name, another's department and a third's salary, and print each with its own label.

What you will do

  1. Build each id from the employee number and the column name the task gives, following the pattern above.
  2. Find each cell with By.id() and read it with getText().
  3. Print the three lines in the order and with the labels the task shows.

Common mistakes

  • Getting the id case wrong

    The ids are case-sensitive: emp3dept does not match emp3Dept and throws NoSuchElementException.

  • Using a column name that is not in the id

    The department column is Dept in the ids, not Department as in the header.

  • Asking for a row that does not exist

    emp6Name does not exist and throws NoSuchElementException; check how many rows the table has first.

In a real test suite

Apps that generate ids from record keys, such as order-1042-status, make table checks simple and stable. When ids are missing, teams often ask developers to add data-testid attributes rather than rely on row positions.

Lesson 64 read the headers. Lesson 66 counts how many rows and columns the table has.

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;

public class Main {
    public static void main(String[] args) throws Exception {
        WebDriver driver = new ChromeDriver();
        driver.get("https://testkru.com/Elements/Tables");
        
        // TODO: Get specific cell values by ID
        // TODO: Print each value
        
        driver.quit();
    }
}