Question

I have the following html document

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

I want to find the link element (link has class called 'mylinkclass') within the book element, this will be based on the text of the span within the same book element.

So it would be something like:

  1. -Find span with text 'mybooktext'

  2. -Navigate up Book div

  3. -Find link with class 'mylinkclass' within book div

This should be done using one xpath statement

Was it helpful?

Solution

In my few this is was your are looking for:

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

//span[contains(text(),'mybooktext')] Find san containing "mybooktext"
/ancestor::div[@class='book'] Navigate up Book div (in any deeps)
//a[@class='mylinkclass'] Find link with class 'mylinkclass' within book div (in any deeps)

Update: change first condition to
//span[(text() ='mybooktext'] if mybooktext is the only text in span

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top