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.
6 Answers
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.
3 Comments
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.
1 Comment
find_element is different from find_elements. Spent some time asking myself why a WebElement had no len attribute.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
Comments
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()
Comments
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)