Java TestNg Selenium-动态查找表列值

Java TestNg Selenium-动态查找表列值,java,selenium,selenium-webdriver,testng,Java,Selenium,Selenium Webdriver,Testng,我正在寻找一种使用testng和selenium动态获取列值的方法。 我有两个表用于显示帐户详细信息,如帐户id、帐户名称、余额、可用余额。 表1用于储蓄账户,表2用于贷款账户。两个表都有余额和可用余额列,但列位置不同 我想要一个方法,它将接受帐户id作为参数(例如:id2),并返回相应帐户的余额?如果我通过了id2考试 应该返回500,如果我通过id4,它应该返回500。 注意:余额和可用余额始终是表的最后一列 <table id=”savings”> <thead>

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

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

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

身份证件
平衡
可用余额
id1
100
123
id2
500
510
身份证件
描述
下一次付款
平衡
可用余额
id3
第一帐户
2018-09-21
100
123
id4
第二帐户
2018-10-25
500
510

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

示例XPath:

//tr[.//td[contains(text(),'id2')]]/tr 
//tr[.//td[contains(text(),'id4')]]/tr
提供余额和可用余额的示例方法:

 // 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();
} 
//将Id值作为id1、id2、id3或id4传递
公共无效finddetailByID(字符串id)
{
List tablerows=driver.findelelements(By.xpath(//tr[.//td[contains(text(),“+id+”)]]/td”);
int rowsize=tablerows.size();
String AvailableBalance=tablerows.get(rowsize.getText();
String balance=tablerows.get(rowsize-1.getText();
} 

使用id='savings'从表中获取值单元格

    System.setProperty("webdriver.chrome.driver", "E:\\chromedriver.exe");
    WebDriver driver = new ChromeDriver();

    driver.get("http://somethingURL");
    driver.manage().window().maximize();

    //insert id here
    String id = "id2";

    //define table by id
    WebElement tbl = driver.findElement(By.id("savings"));

    //get row count
    List<WebElement> rows = tbl.findElements(By.tagName("tr"));

    for(int i=0; i<rows.size(); i++) {
        List<WebElement> row = rows.get(i).findElements(By.tagName("td"));
        int columnCount = row.size();
        //check row contain td, not th
        if(columnCount > 0) {
            // 0=index id column from table with id 'savings'
            String getIDFromTable = row.get(0).getText();
            if(getIDFromTable.equals(id)){
                // 1=index balance column from table with id 'savings'
                String getBalance = row.get(1).getText();
                // 2=index available balance column from table with id 'savings'
                String getAvailableBalance = row.get(2).getText();
                System.out.println(getBalance);
                System.out.println(getAvailableBalance);
                break;
            }
        }
    }               
    driver.quit();
System.setProperty(“webdriver.chrome.driver”,“E:\\chromedriver.exe”);
WebDriver驱动程序=新的ChromeDriver();
驱动程序。获取(“http://somethingURL");
driver.manage().window().maximize();
//在此处插入id
字符串id=“id2”;
//按id定义表
WebElement tbl=driver.findElement(By.id(“savings”);
//获取行计数
列表行=tbl.findElements(按.tagName(“tr”));
对于(int i=0;i 0){
//0=id为“savings”的表中的索引id列
字符串getIDFromTable=row.get(0.getText();
if(getIDFromTable.equals(id)){
//1=id为“savings”的表中的索引余额列
字符串getBalance=row.get(1.getText();
//2=索引id为“储蓄”的表中的可用余额列
字符串getAvailableBalance=row.get(2.getText();
系统输出打印项次(getBalance);
系统输出打印项次(getAvailableBalance);
打破
}
}
}               
driver.quit();

建议使用xpath而不是获取列集合。 这是代码

public void getBalanceById(String id){
   String balance= driver.findElements(By.xpath("(//td[normalize-space(.)='" + id + ']/ancestor::tr/td)[last()-1]")).gettext();
}

你试过什么?你能分享你的代码吗?并通过共享任何相关堆栈跟踪或其他有用的调试信息来解释为什么您共享的代码不起作用?与其发布一个仅链接到另一个答案的答案,不如将其作为副本发布。感谢Suraj Rao和Victor,明白了您的观点,并将更新答案。