使用 HtmlUnit,我应该如何抓取隐藏 div 下的元素(样式 =“显示:无”)?
在这种情况下,我试图获取表中显示的字段的值。第一个单元格是字段名称,第二个单元格是值。我正在使用“for”属性来查找关联的值。
HTML:
<div style="display: none;" id="tab-doc-div">
<div class="tab-container" align="center">
<table class="datatable">
<tbody>
<tr>
<th rowspan="1" colspan="1">
<label for="doc.change.stat">
<font color="">* </font>Action</label>
</th>
<td colspan="2">
Data Change (DTA)
</td>
</tr>
</tbody>
</table>
</div>
我正在使用的 Java/HtmlUnit 代码:
public static String getTextForProperty(HtmlPage page, String property) throws Exception {
List<HtmlLabel> labels = (List<HtmlLabel>)page.getByXPath("//label[@for='" + property + "']");
if (labels.isEmpty()) {
return null;
} else {
return labels.get(0).getParentNode().getNextSibling().asText();
}
}
String myValue = getTextForProperty(myPageObject, "doc.change.stat"); //returns null