【问题标题】:Selenium - How to count the number of rows in a table?Selenium - 如何计算表中的行数?
【发布时间】:2013-10-09 22:18:21
【问题描述】:

我有一个包含“n”行的表格。我要数一数,怎么办?

示例:- 我的表格如下所示。

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>

【问题讨论】:

  • 请提供一些代表您的表格的代码,以便为您提供适当的选择器。
  • 请说明您使用的是哪种语言(Java)以及您是否使用 WebDriver(Selenium IDE?)。
  • Java 和 Selenium IDE。

标签: java selenium selenium-ide


【解决方案1】:

要将行数存储到表中,您可以使用storeXpathCount

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

storeXpathCount 将行数存储到变量 NumberOfRows echo 将返回计数。

【讨论】:

    【解决方案2】:

    我建议如下:

    1) 列出表格的元素(例如,如果您需要计算行数,则使用适当的选择器选择任何行)。

    2) 然后创建网络元素列表

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

    或者:

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

    希望你明白了

    【讨论】:

    • 不幸的是,如果他使用的是 Selenium IDE,这将毫无用处。
    • 另外,您可以将该代码缩短为 int size = driver.findElements().size()
    猜你喜欢
    • 1970-01-01
    • 2016-04-12
    • 2021-03-27
    • 2011-10-07
    • 2012-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多