Skip to content

Instantly share code, notes, and snippets.

@paul
Created October 21, 2010 23:11
Show Gist options
  • Save paul/639569 to your computer and use it in GitHub Desktop.
Save paul/639569 to your computer and use it in GitHub Desktop.
require 'nokogiri'
doc = <<-DOC
<dl>
<dt>Label</dd>
<dd class=other>Other Value</dd>
<dt>Label</dt>
<dd class=foo>Value</dd>
<dd class=foo>Trying to select this dd</dd>
<dd class=other>Other Value</dd>
</dl>
DOC
n = Nokogiri::HTML(doc)
[
"dd.foo",
"dd:last",
"dd.foo:last",
"dd.foo:last-of-type",
"dd.foo:nth-of-type(3)",
"dd[class=foo]:last",
].each do |selector|
puts "Selector: #{selector}"
puts n.css(selector)
puts "\n"
end
Selector: dd.criterion
<dd class="criterion">Value</dd>
<dd class="criterion">Trying to select this dd</dd>
Selector: dd:last
<dd class="other">Other Value</dd>
Selector: dd.criterion:last
Selector: dd.criterion:last-of-type
Selector: dd.criterion:nth-of-type(3)
<dd class="criterion">Trying to select this dd</dd>
Selector: dd[class=criterion]:last
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment