XPATH - 查找子项h3文本具有特定值的元素

时间:2017-06-06 11:43:38

标签: html xpath

enter image description here

我需要使用'就业'来找到所选择的DIV。 h3标签的文字。

我可以使用以下XPATH找到它:

.//div[table[tbody[tr[td[h3[text() = 'Employment']]]]]]/following-sibling::div[@class = 'pbBody']

但这太大又难看,有什么好方法?类似的东西:

parent :: div [h3] follow-sibling :: div,所以没有这个 [table [tbody [tr [td [

HTML文字:



<div class="listRelatedObject customnotabBlock">
<div class="bPageBlock brandSecondaryBrd secondaryPalette">
<div class="pbHeader">
<table cellspacing="0" cellpadding="0" border="0">
<tbody>
<tr>
<td class="pbTitle">
<img class="minWidth" width="12" height="1" title="" style="margin-right: 0.25em;" alt="" src="/img/s.gif"/>
<img class="relatedListIcon" title="Custom" alt="Custom" src="/img/s.gif"/>
<h3 id="a1H9E000000528F_00N2400000IF18T_title">Employment</h3>
</td>
<td class="pbButton">
<td class="pbHelp">
</tr>
</tbody>
</table>
</div>
<div id="DIV_THAT_I_NEED_TO_FIND" class="pbBody">
<div class="pbFooter secondaryPalette">
</div>
</div>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:1)

如果您想要更短的版本,可以尝试

//h3[.="Employment"]/following::div[@class="pbBody"]

答案 1 :(得分:1)

最简单的定位器:

//div[@class="pbBody"][.//h3[text()='Employment']

但最好使用“contains”,因为通常可以将类添加到元素

//div[contains(@class, "pbBody")][.//h3[text()='Employment']

最后一个类似于@Andersson的,但更正了(因为你不需要跟随元素,但是祖先):

//h3[.="Employment"]/ancestor::div[@class="pbBody"]