【问题标题】:How to find an element based on its parent's sibling's child's text with Xpath如何使用 Xpath 根据其父兄弟的子文本查找元素
【发布时间】:2015-01-11 11:57:32
【问题描述】:

有一个这样的html文档:

<div class="main">
   <div class="first">
         **<div id="my_target"></div>**
    </div>
    <div class="second">
       <p class="child">I need to find  preceding or nearest sibling's child with id=my_target based on this text</p>
       <div>...</div>
        <div/>
    <div>....</div>
   </div>

有人可以帮我找到一个带有id=my_target 的元素,并且父元素的兄弟元素 p 包含一些使用 XPath 的特定文本吗?

【问题讨论】:

  • 那会是表哥的文字吗? ;p

标签: c# html xpath


【解决方案1】:

按照你的解释一步一步来:

//div[@id="my_target" and ../following-sibling::div/p="some text"]

演示(使用xmllint):

$ cat test.html
<div class="main">
   <div class="first">
         <div id="my_target">This is what I want to find</div>
    </div>
    <div class="second">
       <p class="child">some text</p>
       <div>...</div>
    </div>
    <div>....</div>
</div>
$ xmllint test.html --xpath '//div[@id="my_target" and ../following-sibling::div/p="some text"]'
<div id="my_target">This is what I want to find</div>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多