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.
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.
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.
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
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()
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))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With