本文介绍了TestNg Selenium-动态查找表列值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种使用testng和硒动态获取列值的方法.我有2个表格,用于显示帐户详细信息,例如帐户ID,帐户名称,余额,可用余额.表1是储蓄帐户,表2是贷款帐户.这两个表都有余额和可用余额列,但列位置不同.

Am looking for a way to fetch the column values dynamically using testng and selenium.I have a 2 tables for displaying account details like account id , account name ,balance , available balance.Table 1 is  for savings accounts and table 2 is for loan accounts.both table has balance and available balance column, but column position is different.

我想要一个单一的方法,该方法将接受帐户id作为参数(例如:id2),并返回相应帐户的余额?例如:如果我通过id2应该返回500,如果我通过id4它应该返回500.注意:余额和可用余额始终是表格的最后一列.

I want a single method which will accept the account id as argument (eg: id2),  and return the  balance of the  corresponding account ? eg: if I pass id2 itshould return 500 ,if  I pass id4 it should return 500.Note : balance and available balance always be the last columns of the table .

<table id="savings">
<thead>
<tr>
<th>id</th>
<th>balance</th>
<th>available balance</th>
</tr>
</thead>
<tbody>
<tr>
<td>id1</td>
<td>100</td>
<td>123</td>
</tr>

<tr>
<td>id2</td>
<td>500</td>
<td>510</td>
</tr>

</tbody>
</table>






<table id="loan">
<thead>
<tr>
<th>id</th>
<th>description</th>
<th>nextpayment</th>
<th>balance</th>
<th>available balance</th>
</tr>
</thead>
<tbody>
<tr>
<td>id3</td>
<td>first account</td>
<td>2018-09-21</td>
<td>100</td>
<td>123</td>
</tr>

<tr>
<td>id4</td>
<td>second account</td>
<td>2018-10-25</td>
<td>500</td>
<td>510</td>
</tr>

</tbody>
</table>

推荐答案

首先,您可以使用id标识td,通过使用id元素可以标识父元素,这样您将获得用唯一ID标识的完整行.

First, you can identify td using id and by using id element you can identify parent element in this way you will get the full row identified with a unique ID.

示例XPath:

//tr[.//td[contains(text(),'id2')]]/tr 
//tr[.//td[contains(text(),'id4')]]/tr

将为您提供余额和可用余额的样本方法:

Sample method which will give you balance and available balance :

 // Pass Id value as id1, id2 , id3, or id4
public void finddetailByID(String id)
{
    List <WebElement> tablerows= driver.findElements(By.xpath("//tr[.//td[contains(text(),'"+id+"')]]/td"));
    int rowsize=tablerows.size();
    String availabebalance=tablerows.get(rowsize).getText();
    String balance=tablerows.get(rowsize-1).getText();
} 

这篇关于TestNg Selenium-动态查找表列值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-25 09:59