1

我有以下html文档

<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>

我想在 book 元素中找到链接元素(链接有名为“mylinkclass”的类),这将基于同一 book 元素中的 span 文本。

所以它会是这样的:

  1. - 使用文本“mybooktext”查找跨度

  2. - 向上导航图书 div

  3. - 在书籍 div 中查找与“mylinkclass”类的链接

这应该使用一个 xpath 语句来完成

4

1 回答 1

10

在我的少数中,这是您正在寻找的:

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

//span[contains(text(),'mybooktext')]查找包含“mybooktext”的 san 向上
/ancestor::div[@class='book']导航 Book div(在任何
//a[@class='mylinkclass']深度) 在 book div 中查找类“mylinkclass”的链接(在任何深度)

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

于 2013-05-09T19:53:30.990 回答