收集表中的所有div id - 使用Java的webdriver

时间:2014-10-14 05:56:59

标签: java html selenium selenium-webdriver

Html代码

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

预期:

我想收集所有div个ID并将其存储为array

我的java代码我在for循环

方面挣扎
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

        }

收集以下id并存储一个数组(trStatus,trFees,trFees1,trFilingReceipt等)

2 个答案:

答案 0 :(得分:1)

希望它有效:

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) {}
        }
}

答案 1 :(得分:0)

试试这个;

 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);
        }
    }

}