Skip to content

Instantly share code, notes, and snippets.

@paul
Created October 21, 2010 23:26
Show Gist options
  • Save paul/639600 to your computer and use it in GitHub Desktop.
Save paul/639600 to your computer and use it in GitHub Desktop.
Selector: dd.foo
<dd class="foo">Value</dd>
<dd class="foo">Trying to select this dd</dd>
Selector: dd:last
<dd class="other">Other Value</dd>
Selector: dd.foo:last-child
Selector: dd.foo:last-of-type
Selector: dd.foo:nth-of-type(3)
<dd class="foo">Trying to select this dd</dd>
Selector: dd.foo:nth-last-of-type(1)
<dd class="foo">Trying to select this dd</dd>
Selector: dd.foo:nth-last-child(1)
<dd class="foo">Trying to select this dd</dd>
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-child",
"dd.foo:last-of-type",
"dd.foo:nth-of-type(3)",
"dd.foo:nth-last-of-type(1)",
"dd.foo:nth-last-child(1)",
].each do |selector|
puts "Selector: #{selector}"
puts n.css(selector)
puts "\n"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment