How to select elements in a matching set of elements xpath

Viewed 19

I have the following html text :

<div>
<p>
 "Text one"
  <br>
 "Text two"
  <br>
 "Text three"
  <br>
 "Text four"
</p>
</div>

I need to select only "Text two" and "Text three" using xpath. So far I have the following xpath which selects the entire text:

 text = doc.xpath('/div/p/text()')

Though I expect the result to be :

 Text two
 Text three

How can I achieve this?

1 Answers

Assuming that the location of the text within the tree is what determines your targets, try changing you xpath expression to

//div/p/text()[position()> 1 and position() < 4]
Related