Xpath: how do you select the second text node (specific text node)

view html page

<html>
apple

orange

drugs

</html>

how to choose orange color using xpath?

/html/text()[2]

does not work.

+3
source share
2 answers

You cannot do this directly by selecting. You need to call the xpath line function to cut the text () to get the desired line

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

here is a list of string functions

+3
source

If the lines are split <br>, then they work

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

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


All Articles