Skip to content
CodeKruPro

Read Specific Cells from the Employee Table

71/80

Read Specific Cells from the Employee Table

Each data row is a tr in tbody. Cells in a row are td elements. Count from the first data row.

Task:

  1. Print the name in the first data row
  2. Print the department in the third data row
  3. Print the salary in the fourth data row

Print the lines below exactly.

Expected output:

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

About this lesson

Get table cell text in Selenium Python: pick a row, then pick a cell by index

Selenium Python AssignmentsAdvancedTables

How it works

A table in HTML is nested boxes. The table holds a tbody, the tbody holds tr rows, and each row holds td cells. Selenium has no special "cell" call. You find the rows, choose one, then look inside it for its cells.

In Python, find_elements always returns a list. Square brackets pick one item, and counting starts at 0, so the first data row is rows[0] and the third is rows[2]. The same rule applies to cells inside a row. A missing index raises IndexError: list index out of range, a plain Python error.

Searching from a row element keeps the search inside that row. Be careful with XPath: a path that starts with a double slash scans the whole page even when called on a row, so you get the first cell of the table every time. Read the cell with text, which takes no parentheses.

About the assignment

Open the Tables page and pull three single values out of the employee table: a name, a department and a salary, each from a different row.

What you will do

  1. Find all body rows of the employee table into one list, using the table id and tbody so the header row is left out.
  2. Pick the first, third and fourth rows from that list. Remember the first row is index 0.
  3. Inside each chosen row, find its td cells and pick the one you need: name is first, department third, salary fourth.
  4. Join a label and the cell text with +, then print each of the three lines.

Common mistakes

  • Counting rows from 1

    Asking for the fifth item in a list of five, with an index of 5, fails with IndexError: list index out of range. The last of five rows is index 4. A negative index such as -1 reaches the last row without counting.

  • Searching the driver, not the row

    Calling find_elements on driver for td returns the cells of the whole page, in page order. Index 2 then gives you a cell from the first row, not the third. Call it on the row element.

  • Taking cell positions from the screen

    Counting columns by eye lands one off. Name, age, department and salary are four cells, so department sits at index 2. Listing every cell text once while you explore settles it.

In a real test suite

In a pytest suite, teams hide this behind a page object method that takes a row number and a column name and returns the text. A change to the table layout is then fixed in one place.

The next lesson counts rows and columns with len, and the one after loops over every row.

To try one of these calls on any page without a task, open the Online Selenium Python Compiler.

Starter code

The editor above opens with this code. Fill in the TODO lines, then run and submit it.

main.py
from selenium import webdriver
from selenium.webdriver.common.by import By

driver = webdriver.Chrome()
try:
    driver.get("https://testkru.com/Elements/Tables")
    # Print the three cells from #employeeTable
finally:
    driver.quit()