This is the generic way to do it:
require 'nokogiri'
doc = Nokogiri::HTML(<<EOT)
<html>
<body>
<form>
<select id="menu_id">
<option value="foo">foo</option>
</select>
</form>
</body>
</html>
EOT
doc.search('#menu_id option').any?{ |option| option.text == 'foo' } # => true
That looks to see if the text, not the value, is "foo"
.
doc.search('#menu_id option').any?{ |option| option.text['foo'] } # => true
That looks to see if the text, not the value, contains the sub-string "foo"
.
doc.search('#menu_id option').any?{ |option| option['value'] == 'foo' } # => true
That looks to see if the value
parameter matches the word "foo"
.
Similarly, they'll tell you whether something doesn't match:
doc.search('#menu_id option').any?{ |option| option.text == 'bar' } # => false
doc.search('#menu_id option').any?{ |option| option.text['bar'] } # => false
doc.search('#menu_id option').any?{ |option| option['value'] == 'bar' } # => false
I would probably rely on Nokogiri's implementation of the jQuery extensions that @gmcnaughton mentioned but that's how I am. YMMV.