About this lesson
How to count rows and columns of a web table in Selenium Python using len
Selenium Python AssignmentsAdvancedTables
How it works
Selenium does not have a "row count" method. Instead, find_elements returns a Python list, and the built-in len tells you how many items that list holds. If the selector matches nothing, you get an empty list and len gives 0. No exception is raised, which makes counting a safe way to ask "is anything there?".
The hard part is choosing what to count. The employee table has one header row and several body rows. A selector that ends at the table plus tr matches both kinds, so the count is one too big. Limiting the search to tbody counts employees only. For columns, the header cells in the thead are the cleanest source, since each column has exactly one of them.
On this page there are two tables, so a bare table or tr search would mix them. Starting the selector at the id of the employee table avoids that. Also watch the types. len returns an int, and gluing an int onto a string with + raises TypeError with the message can only concatenate str (not "int") to str. Convert it with str first, or use an f-string.
About the assignment
Report two counts for the employee table: how many data rows it has and how many columns it has. You must count through the page, not type the numbers yourself.
What you will do
- Find every body row of the employee table and keep the list in a variable.
- Find every header cell in the table head and keep that list as well.
- Ask
lenfor the size of each list. - Turn each number into text before joining it to its label, then print one line for rows and one for columns.
Common mistakes
Counting the header as data
Matching all
trelements under the table includes the header row, so you are off by one. Addtbodyto the selector, or subtract one only if you have a reason to.Joining an int to a string
A line that adds the result of
lenstraight to a label stops withTypeErrorwith the messagecan only concatenate str (not "int") to str. Wrap the number instr.Counting td cells for columns
Every body row has its own cells, so counting
tdgives rows times columns, not columns. The header cells match one for one with the columns.
In a real test suite
Row counts show up in real suites as a quick check after a filter or a search: apply the filter, then compare the count with what the data says. Teams usually wrap the selector in a helper so the tbody detail lives in one function.
After counting, you can read each row. The next lesson does that with a loop.
Related lessons
- Find Every Radio ButtonAlso works with a list from
find_elements, using radio buttons. - Read Specific Cells from the Employee TablePicks single cells from the same table by index.
- Read Every Employee RowWalks the same list of body rows with a
forloop.
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 data-row count and the column count
finally:
driver.quit()