Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 392
Kv.senthilkumar Avatar asked Feb 12 '13 11:02

Kv.senthilkumar


5 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.

like image 140
Santoshsarma Avatar answered Oct 01 '22 07:10

Santoshsarma


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.

like image 31
Adam Wildavsky Avatar answered Sep 30 '22 07:09

Adam Wildavsky


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
like image 26
Andriy Ivaneyko Avatar answered Oct 01 '22 07:10

Andriy Ivaneyko


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()
like image 43
Hans Wouters Avatar answered Oct 03 '22 07:10

Hans Wouters


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))
like image 2
user3036030 Avatar answered Oct 03 '22 07:10

user3036030