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
- Find all body rows of the employee table into one list, using the table id and
tbodyso the header row is left out. - Pick the first, third and fourth rows from that list. Remember the first row is index 0.
- Inside each chosen row, find its
tdcells and pick the one you need: name is first, department third, salary fourth. - 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_elementsondriverfortdreturns 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.
Related lessons
- Read the Employee Table HeadersReads the header cells of the same employee table, which shows you how
thdiffers fromtd. - Count Employee Rows and Columns with len()Counts the body rows and header cells of this table with
len. - Read Every Employee RowLoops over every body row instead of picking rows by index.
- Find Elements with CSSBuilds the CSS selectors that make a table search like this short.
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 the three cells from #employeeTable
finally:
driver.quit()