XPATH - how to select this part of the text?

Code on the page:

<div class='container'> <p> <b>Address:</b> Some address<br /> <b>Phone:</b> phone1, phone2<br /> <b>E-mail: </b><a href='mailto:somemail' >somemail</a><br /> <b>Site:</b><a href='somesite'>somesite</a> </p> </div> 

I need to select the text after </b> and before <br /> using XPATH. In this case, I will need to get "Some address" or "phone1, phone2", etc.

"Some address" should be in $ var1 'phone1, phone2' in $ var2

I tried with .//*[@class="container"]/p/text()[1] , this did not work.

+4
source share
2 answers

Try this one

 /div[@class="container"]/p/descendant-or-self::text()[ not(ancestor::b) and normalize-space(.) != "" ] 

This will select text nodes in the P element tree (from a div with the class attribute "container") that are not in the B element tree or are empty, for example. it will give you

  • "Some addresses,"
  • "phone1, phone2",
  • "somemail" and
  • "somesite".

Demo

Also check out this XPath tutorial .

+3
source

The closest I can reach:

 '//div[@class="container"]/p/text()[preceding::b[contains(text(),"Address")] and following-sibling::b[contains(text(),"Phone")]]' 

OR as @Gordon suggested

  '//div[@class="container"]/p/text()[following::b[contains(text(),"Phone")] and normalize-space(.)!=""]' 

:)

0
source

Source: https://habr.com/ru/post/1402889/


All Articles