Skip to content
CodeKruPro

Count Employee Rows and Columns with len()

72/80

Count Employee Rows and Columns with len()

Data rows are the body rows. Columns are the header cells. Use len.

Task:

  1. Count #employeeTable tbody tr
  2. Count the header cells
  3. Print both numbers

Print the lines below exactly.

Expected output:

Data rows: 5
Columns: 4

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

  1. Find every body row of the employee table and keep the list in a variable.
  2. Find every header cell in the table head and keep that list as well.
  3. Ask len for the size of each list.
  4. 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 tr elements under the table includes the header row, so you are off by one. Add tbody to 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 len straight to a label stops with TypeError with the message can only concatenate str (not "int") to str. Wrap the number in str.

  • Counting td cells for columns

    Every body row has its own cells, so counting td gives 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.

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 data-row count and the column count
finally:
    driver.quit()