我在计算以下 XPATH 时遇到了问题。还有其他几个具有类似属性的元素(包括 ID,但它不是我的页面),确保选择正确元素的唯一方法是通过其相邻标签“版权”获取它。这是 HTML:
<div class="row">
<div class="label">Copyright :</div>
<div id="container-id" class="field" >2011 Homeland Ent</div>
</div>
<div class="row">
<div class="label">Another Label :</div>
<div id="container-id" class="field" >Another Entry</div>
</div>
请注意,由于格式错误的 HTML,//*[@id="container-id"]
在这里是不够的。获取文本“2011 Homeland Ent”的正确 XPATH 是什么?
最佳答案
也许是这样的(我不确定“版权”文本是否是搜索的一部分,无论如何都包括在内
//*[@class="label" and starts-with(., "Copyright")]/following-sibling::*[@id="container-id"]
关于html - 使用 XPath 通过相邻标签查找元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8903616/