Skip to content

Instantly share code, notes, and snippets.

@logrusorgru
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save logrusorgru/63ba4fed0485e450c084 to your computer and use it in GitHub Desktop.
Save logrusorgru/63ba4fed0485e450c084 to your computer and use it in GitHub Desktop.
Nokogiri and dollar sign $

Nokogiri does not understand the dollar sign $ in selectors.

Some example:

require 'nokogiri'

html = %Q{
  <!DOCTYPE html>
  <body>
  <div id='im$the$div'></div>
  </body>}

page = Nokogiri::HTML( html )

page.css('#im$the$div')
# => Nokogiri::CSS::SyntaxError: unexpected '$' after '[#<Nokogiri::CSS::Node:0x000000039fe5b8 @type=:CONDITIONAL_SELECTOR, @value=[#<Nokogiri::CSS::Node:0x000000039fe608 @type=:ELEMENT_NAME, @value=["*"]>, #<Nokogiri::CSS::Node:0x000000039fe748 @type=:ID, @value=["#im"]>]>]'

but you can use XPath selector:

page.xpath("//div[@id='im$the$div']")
# => [#<Nokogiri::XML::Element:0x1a8f59c name="div" attributes=[#<Nokogiri::XML::Attr:0x1a8f344 name="id" value="im$the$div">] children=[#<Nokogiri::XML::Text:0x1a8e598 "\n">]>]
page.xpath("//div[@id='im$the$div']")[0].attr(:id)
# => "im$the$div"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment