How to count no of rows in table from web application using selenium python webdriver
Asked Answered
S

6

17

How to count the rows in the table from web application by using selenium python web driver. Here we can retrieve all data in the table from web application but couldn't count the rows and columns, please give me idea of how to do this.

Slapup answered 12/2, 2013 at 11:4 Comment(0)
L
21

Try some thing like this

int rowCount=driver.findElements(By.xpath("//table[@id='DataTable']/tbody/tr")).size();

int columnCount=driver.findElements(By.xpath("//table[@id='DataTable']/tbody/tr/td")).size();

FYI : This is the implementation in java.

Livelihood answered 12/2, 2013 at 16:15 Comment(3)
i'm using python web driver. This size() function is not working there.Slapup
See this link : selenium.googlecode.com/git/docs/api/py/webdriver_remote/… -------> I think in python len is the method to find length/size of the listLivelihood
TypeError: 'dict' object is not callableManofwar
G
13

Santoshsarma's answer is close to correct for Java, but it will count the number of cells rather than the number of columns. Here's a Python version that will count the number of columns in the first row:

row_count = len(driver.find_elements_by_xpath("//table[@id='DataTable']/tbody/tr"))
column_count = len(driver.find_elements_by_xpath("//table[@id='DataTable']/tbody/tr/td"))

If your table has a header you could count the columns there instead.

I haven't tried using storeXpathCount.

Generic answered 25/5, 2014 at 22:34 Comment(1)
A tip for future readers: remember that find_element is different from find_elements. Spent some time asking myself why a WebElement had no len attribute.Zobias
N
4

You can do it by using find_elements_by or execute_script.

Retrieving count by applying len on result of find_elements_by is correct way, however in other turn getting count of rows and columns by using execute_script is quite faster then getting len from result of find_elements_by method call:

rows_count = driver.execute_script("return document.getElementsByTagName('tr').length")
columns_count = driver.execute_script("return document.getElementsByTagName('tr')[0].getElementsByTagName('th').length")

Below is performance metrics of using find_elements_by vs execute_script approach:

from timeit import timeit

# Time metrics of retrieving 111 table rows
timeit(lambda:driver.execute_script("return document.getElementsByTagName('tr').length"), number=1000)
# 5.525
timeit(lambda:len(driver.find_elements_by_tag_name("tr")), number=1000) 
# 9.799
timeit(lambda:len(driver.find_elements_by_xpath("//table/tbody/tr")), number=1000)
# 10.656

# Time metrics of retrieving 5 table headers
timeit(lambda:driver.execute_script("return document.getElementsByTagName('tr')[0].getElementsByTagName('th').length"), number=1000)
# 5.441
timeit(lambda:len(driver.find_elements_by_tag_name("th")), number=1000)
8.604
timeit(lambda:len(driver.find_elements_by_xpath("//table/tbody/th")), number=1000)
# 9.336
Noles answered 3/8, 2016 at 9:19 Comment(0)
M
2

Selenium has a function for counting the XPath result, see http://release.seleniumhq.org/selenium-core/1.0/reference.html. According to this you can use storeXpathCount.

I am using the DefaultSelenium class from the selenium-java-client-driver, where I can use (also in java) the following:

int rowCount = selenium.getXpathCount("//table[@id='Datatable']/tbody/tr").intValue()
Majesty answered 28/2, 2014 at 14:35 Comment(0)
N
2

For python users the simple way of counting a tag is as follows.

!#use find_elements_by_xpath

test_elem = self.driver.find_elements_by_xpath("//div[@id='host-history']/table[1]/tbody[1]/tr")

!#then check the len of the returnes list

print (len(test_elem))
Naamana answered 6/7, 2017 at 13:26 Comment(0)
M
1

As of 2022

The best way I found was as follow. Where my table has many tr elements

Get the full path and

path1="/html/body/div[5]/div[3]/div/div[1]/div/div/div[4]/form/div[3]/div[2]/div/table/tbody"
element1=driver.find_element(By.XPATH,path1).find_elements(By.CSS_SELECTOR, 'tr')
number_of_rows = len(element1)
Manofwar answered 15/9, 2022 at 18:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.