Skip to content
CodeKruPro

Read Every Employee Row

73/80

Read Every Employee Row

Loop over every body row. Print the name and the department. This lesson prints all five employees.

Task:

  1. Read each data row of #employeeTable
  2. Print Name - Department for every row

Print the lines below exactly.

Expected output:

John Smith - Engineering
Jane Doe - Marketing
Bob Johnson - Sales
Alice Williams - Engineering
Charlie Brown - Marketing

About this lesson

Loop through table rows in Selenium Python and print each employee

Selenium Python AssignmentsAdvancedTables

How it works

Reading a whole table follows one pattern. First, find the body rows once. Then use a for loop to visit each row. Inside the loop, look for the td cells of that row only, pick the ones you want by index, and use their text. The loop variable is a real WebElement, so everything you know about elements applies to it.

A loop handles any number of rows. If the page later shows eight employees, the same code prints eight lines. That is the gain over hard-coded indexes. When you also need the position of each row, enumerate gives you the index and the row together, and a list comprehension can collect values into a new list in a single line.

Each cell read is one more call to the browser, so a large table is slow to walk. Keep the page stable while you loop. If a script clicks something that redraws the table in the middle, the rows you stored go stale and the next read raises StaleElementReferenceException. Here the table is static, so a plain loop is safe.

About the assignment

Print one line for every employee in the table, showing the name and the department joined by a dash with spaces around it. All five employees must appear, in the order the page shows them.

What you will do

  1. Collect the body rows of the employee table into a list.
  2. Start a for loop over that list.
  3. Inside the loop, find the cells of the current row. Name is the first cell and department is the third.
  4. Build the text from the two cell values with the separator between them and print it, once per pass.

Common mistakes

  • Searching the driver inside the loop

    If the cell search runs on driver, every pass reads the same first cells of the page. The loop prints one employee five times. Search from the loop variable.

  • Printing the whole row text

    A row's text joins all four cells with spaces, including age and salary. You need two cells, so read the cells one at a time.

  • Indenting the print outside the loop

    Python decides loop membership by indentation. A print at the left edge runs once, after the loop, and shows only the last employee.

In a real test suite

Real suites turn a table into data: a list of dicts, one per row, which a test then compares with an expected list. That reads better than loose prints, and a failing diff shows exactly which employee is wrong.

Next, the track leaves tables and moves to cookies and page source.

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 every employee as "Name - Department"
finally:
    driver.quit()