Home > Mobile >  get text part upto a specific tag in xpath
get text part upto a specific tag in xpath

Time:10-21

i am trying to get text till 1st <.hr>(ignore dot) tag using Xpath

<div class="entry">
   <p> some text</p>
   <p> some text2</p>
   <p> some text3</p>
   <p> some text4</p>
   <hr>(get text part before this hr tag)
   <p> some text5</p>
   <hr>
   <p> some text6</p>
</div>

tried this

//hr[1]/ancestor::div[@class="entry"]/text()

and some similar variants but couldn't get the expected output

CodePudding user response:

Something along these lines will give you the set of nodes before the hr node

//div[@class="entry"]/*[not(preceding-sibling::hr | self::hr)]

It will list those nodes that

  • are children of the div with class name "entry",
  • are not preceded by a node named hr and
  • are not themselves a hr node
  • Related