2013-05-09 136 views
0

我有以下的HTML文档的XPath搜索多个嵌套元素

<div class="books"> 
    <div class="book"> 
    <div> 
     there are many deep nested elements here, somewhere there will be one span with some text e.g. 'mybooktext' within these 
     <div> 
     <div> 
      <div> 
      <span>mybooktext</span> 
      </div> 
     </div> 
    </div> 
    </div> 
<div> 
there are also many nested elements here, somewhere there will be a link with a class called 'mylinkclass' within these. (this is the element i want to find) 
<div> 
    <div> 
    <a class="mylinkclass">Bla</a> 
    </div> 
</div> 
</div> 
</div> 
<div class="book"> 
<div> 
there are many deep nested elements here, somewhere there will be one span with some  text e.g. 'mybooktext' within these 
    <div> 
    <span>mybooktext</span> 
    </div> 
<div> 
</div> 
<div> 
there are also many nested elements here, somewhere there will be a link with a class called 'mylinkclass' within these. (this is the element i want to find) 
<div> 
    <a class="mylinkclass">Bla</a> 
</div> 
</div> 
</div> 
<div class="book"> 
same as above 
</div> 
</div> 

我想找到的链接元素(链路级称为“mylinkclass”),这将是基于文本的书元素中在同一书籍元素内的跨度。

因此,这将是这样的:

  1. - 查找跨度文本 'mybooktext'

  2. -Navigate了书的div

  3. 与类中的 'mylinkclass' - 查找链接book div

这应该使用一个xpath s来完成tatement

+0

您能否重新考虑您的示例?它会匹配你的xpath请求。 “mybooktext”是跨度中的唯一文本还是文本的一部分? – 2013-05-09 19:37:03

+0

'mybooktext'将是唯一的文本。 – user1786107 2013-05-09 19:43:57

回答

4

在我的几个,这是在您正在寻找:

" //span[contains(text(),'mybooktext')] 
      /ancestor::div[@class='book'] 
      //a[@class='mylinkclass']" 

//span[contains(text(),'mybooktext')]查找包含SAN “mybooktext”
/ancestor::div[@class='book']向上导航书格(在任何深处)与类
//a[@class='mylinkclass'] Find链接“ mylinkclass'内的书div(在任何深度)

更新: 更改第一个条件为
//span[(text() ='mybooktext']如果mybooktext是范围中唯一的文本

+0

非常感谢。我现在不在电脑,但明天会测试,并将其标记为答案。再次感谢 – user1786107 2013-05-09 19:59:52

+0

您在此期间尝试过吗? – 2013-05-10 17:15:57