Selenium - How to count the number of rows in a table?

穿精又带淫゛_ 提交于 2019-12-06 06:48:05

问题


I have a table with "n" number of rows. I need to count them, how to do?

Example:- My table looks like the below entry.

Type        Balance Date received       Date returned   Payment method  Amount
General     Default 10/01/2013 08:53:20                 Cash            $ 10.00

HTML of my table looks like this. <th> is table title row and <td> are the entries done by the user. Everytime the user add the new entry, the count will increase and display a message "3 found for account 6478 ". where 3 is no of rows in the table.   
<fieldset>
<table id="listBalances" class="listTable" width="100%">
<thead>
<tr>
<th>Type</th>
<th>Balance</th>
<th>Date received</th>
<th>Date returned</th>
<th>Payment method</th>
<th>Amount</th>
</tr>
</thead>
<tbody>
<tr>
<tr class="alt">
<td>General Deposit</td>
<td>Default for RTB</td>
<td>09/30/2013 06:58:41</td>
<td/>
<td>Cash</td>
<td>$ 5.00</td>

回答1:


To store Number of rows into table you can use storeXpathCount

storeXpathCount | id=listBalances | NumberOfRows
echo  | ${NumberOfRows}

storeXpathCount will store rows count into variable NumberOfRows And echo will return count.




回答2:


I would suggest the following:

1) make list of elements of your table (e.g if you need to count your row count then select any row using appropriate selector).

2) then create list of web elements

List<WebElement> columnElments = driver.findElements(By.cssSelector("...blablabla..."));
int rowNumer = columnElmments.size();

or either:

@FindBy
            (how = How.CSS, using = "blablablba")
List<WebElement> col;

int rowNumer =col.size();

Hope you got the idea



来源:https://stackoverflow.com/questions/19155430/selenium-how-to-count-the-number-of-rows-in-a-table

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!