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并将其存储在数组
我的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 等)
最佳答案
希望它有效:
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) {}
}
}
关于java - 使用Java收集表中的所有div id - webdriver,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26353574/