简体   繁体   中英

XPath find tag contains and then get the span tag below

I am new to XPath and wouder how to get the <span> tag in this HTML code.

I am using the tag contains feature. I match the right tag, but I dont know how to get the <span> tag below the <em> so that I can get the date:

//em[contains(text(), "Datum för upprättande:")]


<li class="clear">
  <em class="">Datum för upprättande:</em>
  <span class="">19-11-2008</span>
</li>

您可以使用following-sibling

//em[contains(text(), "Datum för upprättande:")]/following-sibling::span

As you specified Nokogiri:

require 'nokogiri'

html = '<li class="clear">
<em class="">Datum för upprättande:</em>
<span class="">19-11-2008</span>                                                                                                                                                     
</li>'  

doc = Nokogiri::HTML(html)                                                                                                                                                           
em = doc.xpath('//em[contains(text(), "Datum för upprättande:")]')                                                                                                                   
puts em[0].next_element.content # => 19-11-2008

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM