user2805845 user2805845 - 8 months ago 145
Ruby Question

How to get row count from a table using ruby capybara?

In Java, we use:

WebElement tablename=driver.findElement(By.xpath("table_xpath"));

WebElement<List> rowcount=tablename.findelements(By.tagname("tr"));


How to find row count in Ruby Capybara for above case?


You can find all the elements matching a selector using #all so

page.all(:css, 'table tr').size

will count all the table rows in the page. If there are multiple tables and you want for a specific table you can increase the specificity of the CSS selector. You can also find the specific table and then call all on that

table = page.find(:css, 'table#my_table_id')
row_count = table.all(:css, 'tr').size