-
Notifications
You must be signed in to change notification settings - Fork 1
Traversing
boooz edited this page May 17, 2012
·
3 revisions
Skyscraper nodes has following methods helping with traversing:
Returns first node matches provided selector
>> Skyscraper::fetch("http://rubyonrails.org").first("a")
# => #<Skyscraper::Node::Base:0xb88c524 @element=#<Nokogiri::XML::Element:0x5c4629c name="a" attributes=[#<Nokogiri::XML::Attr:0x5b0158a name="href" value="/">] children=[#<Nokogiri::XML::Text:0x5b01332 "Overview">] Returns an array of nodes, matches provided selector
>> Skyscraper::fetch("http://rubyonrails.org").find("#Nav a").map(&:text)
# => ["Overview", "Download", "Deploy", "Bugs/Patches", "Screencasts", "Documentation", "Ecosystem", "Community", "Blog"]Returns an array of node childrens, matching selector if provider.
>> Skyscraper::fetch("http://rubyonrails.org").first("#Nav").children.map(&:html)
# => ["Overview", "Download", "Deploy", "Bugs/Patches", "Screencasts", "Documentation", "Ecosystem", "Community", "Blog"]
>> Skyscraper::fetch("http://rubyonrails.org").first("#Nav").children("a:first-child").map(&:html)
# => ["Overview"]It doesn't returns children of childrens.
Returns node parent
>> Skyscraper::fetch("http://rubyonrails.org").first("#Nav").parent.id
# => "Header"Returns node parents, matching selector if provided.
>> Skyscraper::fetch("http://rubyonrails.org").first("#Nav").parents.map(&:tag)
# => ["div", "div", "body", "html"]
>> Skyscraper::fetch("http://rubyonrails.org").first("#Nav").parents("div").map(&:tag)
# => ["div", "div"]Returns an array of node siblings
>> Skyscraper::fetch("http://rubyonrails.org").first("#Nav a").siblings.map(&:text)
# => ["Download", "Deploy", "Bugs/Patches", "Screencasts", "Documentation", "Ecosystem", "Community", "Blog"]If an elemenent have href attribute you can fetch page on which it indicates.
>> Skyscraper::fetch("http://rubyonrails.org").find("#Nav a")[1].follow.first("title").text
# => "Ruby on Rails: Download"