我如何在以下html代码中获取父ID。使用描述类名称我想获取该div的id.there卡片盒类名称对于所有div都是通用的,并且在运行时生成id。
<div id="2321-79" class="card-box"></div>
<div id="2321-32" class="card-box"></div>
<div id="2321-33" class="card-box"></div>
<div id="2321-81" class="card-box"></div>
<div id="2321-74" class="card-box"></div>
<div id="2321-59" class="card-box"></div>
<div id="2321-80" class="card-box"></div>
<div id="2321-35" class="card-box"></div>
<div id="2321-84" class="card-box">
<div class="card overdue white">
<div class="summary"></div>
<div class="info" style="display: none;"></div>
</div>
</div>
<div id="2321-82" class="card-box">
<div class="card overdue white">
<div class="summary">
<a class="pic" href="/profiles/219"></a>
<div class="detail">
<div class="title"></div>
<span class="description"></span>
</div>
</div>
<div class="info" style="display: block;">
<div class="team">
<table width="100%" style="padding: 0px 10px">
<tbody>
<tr>
<td class="from" colspan="4">
From
<a href="/profiles/219"></a>
to
<a href="/profiles/1"></a>
</td>
</tr>
<tr>
<td width="20%"></td>
<td width="50%"></td>
<td width="7%"></td>
<td width="20%"></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
</tbody>
</table>
</div>
<div class="actions">
<form method="post" data-remote="true" action="/commitments/get_view" accept-charset="UTF-8">
<div style="margin:0;padding:0;display:inline"></div>
<input id="col_type" type="hidden" value="col_my_work_commitments" name="col_type"></input>
<input id="card_id" type="hidden" value="174" name="card_id"></input>
<input id="state" type="hidden" value="published" name="state"></input>
<table width="100%">
<tbody>
<tr class="actions">
<td>
<button id="commitments-identifier" type="submit" name="btn_discuss"></button>
</td>
</tr>
</tbody>
</table>
</form>
</div>
</div>
</div>
</div>
</section>
为此我尝试了这段代码
String val1 = "Final";
List<WebElement> resultList1 = card1.findElements(By.className("description"));
for (WebElement resultItem : resultList1){
String tabname=resultItem.getText();
if(tabname.contentEquals(val1))
{
System.out.println(description.getAttribute("id"));
resultItem.click();
break;
}
}
答案 0 :(得分:1)
假设您有一些WebElement el
,并且想要获得其父WebElement
,则可以使用:
el.findElement(By.xpath(".."))
要获取 元素的id
,您可以使用:
el.findElement(By.xpath("..")).getAttribute("id")