tags:

views:

422

answers:

2

consider a html page

<html>
apple

orange

drugs

</html>

how can you select orange using xpath ?

/html/text()[2]

doesn't work.

+1  A: 

You cant do it directly by selecting. You need to call an xpath string function to cut the text() to get the string you want

substring-after(/html/text()," ") // something like this,

here is a list of string functions

Andrew Keith
A: 

If the strings are separated with <br> it works

  doc = Nokogiri::HTML("""<html>
  apple
  <br>
  orange
  <br>
  drugs
  </html>""")
  p doc.xpath('//text()[2]') #=> orange
Adrian