TestNg Selenium- Find table column value dynamical

2019-07-29 05:04发布

问题:

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.

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

回答1:

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. Sample 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();
} 


回答2:

Get value cell from table with 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();


回答3:

Recommend using the xpath rather getting the column collection. Here is the code.

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