Prabu
Prabu

Reputation: 3728

Collect all the div id inside the table - webdriver using Java

Html Code

<table id="tblRenewalAgent" cellspacing="0" cellpadding="0" border="0">
<tbody>
<tr>
<td>
<div class="form-row">
<div id="trStatus" style="">
<div id="trFees" class="form-row" style="">
<div id="trFees1" class="form-row ctrl-column" style="">
<div id="trFilingReceipt" class="form-row" style="">
<div id="trComments" class="form-row" style="">
<div id="trContact" class="form-row" style="">
<div id="trEmail" class="form-row" style="">
<div id="trPhone" class="form-row" style="">
<div id="trCell" class="form-row" style="">
<div class="form-row">
<div class="form-row ctrl-column">
<div id="trAmountPaid">
<div id="trBalanceDue" class="form-row">
</td>
</tr>
</tbody>
</table>

Expected:

i want to collect all the div id and store it an array

my java code i'm struggled in side that for loop

public void gettingFee() {
        WebElement ptoFeeTable = driver.findElement(By.xpath("//*[@id='tblRenewalAgent']/tbody"));
        List<WebElement>allRows = ptoFeeTable.findElements(By.tagName("tr"));
        for (int i=0;i<=allRows.size();i++){

         //collect all the div id's

        }

collect the following id's and store it an array (trStatus,trFees,trFees1,trFilingReceipt etc.,)

Upvotes: 0

Views: 878

Answers (2)

Sai Ye Yan Naing Aye
Sai Ye Yan Naing Aye

Reputation: 6738

Try this;

 public void gettingFee() {
    List<WebElement> divList = driver.findElements(By.tagName("div"));
    List<String> idList = new ArrayList<String>();
    for (WebElement e : divList) {
        if(!e.getAttribute("id").isEmpty()) {
            String s = e.getAttribute("id");
            idList.add(s);
            System.out.println(s);
        }
    }

}

Upvotes: 0

Priyanshu
Priyanshu

Reputation: 3058

Hope it works:

public void gettingFee() {
        List<String> allIds = new ArrayList<String>();
        WebElement tdElement = driver.findElement(By.xpath("//*[@id='tblRenewalAgent']/tbody/tr/td"));
        List<WebElement> allDivElements = tdElement.findElements(By.tagName("div"));
        for(int i = 0; i<allDivElements.size(); i++){
           try{
           String id = allDivElements.get(i).getAttribute("id");
           allIds.add(id );
           } catch(Exception e) {}
        }
}

Upvotes: 2

Related Questions