How to select nodes by matching text How to select nodes by matching text ruby ruby

How to select nodes by matching text


Nokogiri can do this (now) using jQuery extensions to CSS:

require 'nokogiri'html = '<html>  <body>    <p>foo</p>    <p>bar</p>  </body></html>'doc = Nokogiri::HTML(html)doc.at('p:contains("bar")').text.strip=> "bar"


Here is an XPath that works:

require 'nokogiri'doc = Nokogiri::HTML(DATA)p doc.xpath('//li[contains(text(), "Apple")]')__END__<p>A paragraph <ul><li>Item 1</li><li>Apple</li><li>Orange</li></ul></p>


Try using this XPath:

p = doc.xpath('//p[//*[contains(text(), "Apple")]]')