How to count the number of rows in a table in Selenium with python?

We can count the total number of rows in a table in Selenium. The rows of a table are represented by <tr> tag in html code. To get all the rows, we shall use the locator xpath and then use find_elements_by_xpath method. The list of rows will be returned. Next we need to compute the size of the list with the help of len method.

The html code snippet of a table row count is as described below −




Code Implementation for getting row count.

from selenium import webdriver
#browser exposes an executable file
#Through Selenium test we will invoke the executable file which will then
#invoke actual browser
driver = webdriver.Chrome(executable_path="C:\chromedriver.exe")
# to maximize the browser window
#get method to launch the URL
#to refresh the browser
# identifying the number of rows having <tr> tag
rows = driver.find_elements_by_xpath("//table/tbody/tr")
# len method is used to get the size of that list
#to close the browser

Updated on: 29-Jul-2020

10K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started