How to select nodes by matching text

RubyNokogiri

Ruby Problem Overview


If I have a bunch of elements like:

<p>A paragraph <ul><li>Item 1</li><li>Apple</li><li>Orange</li></ul></p>

Is there a built-in method in Nokogiri that would get me all p elements that contain the text "Apple"? (The example element above would match, for instance).

Ruby Solutions


Solution 1 - Ruby

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"

Solution 2 - Ruby

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>

Solution 3 - Ruby

You can also do this very easily with Nikkou:

doc.search('p').text_includes('bar')

Solution 4 - Ruby

Try using this XPath:

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionZandoView Question on Stackoverflow
Solution 1 - Rubythe Tin ManView Answer on Stackoverflow
Solution 2 - RubyAaron PattersonView Answer on Stackoverflow
Solution 3 - RubyTomView Answer on Stackoverflow
Solution 4 - Rubyandre-rView Answer on Stackoverflow