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
- Collect the body rows of the employee table into a list.
- Start a
forloop over that list. - Inside the loop, find the cells of the current row. Name is the first cell and department is the third.
- 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
textjoins 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.
Related lessons
- Read Specific Cells from the Employee TableReads single cells by index, the building block used inside this loop.
- Count Employee Rows and Columns with len()Counts the same body rows, so you know how many passes to expect.
- Recover After an Element Goes StaleShows what happens when stored elements go stale after the page changes.
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.
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()