1. Computing

The Link Class

By

Updated July 14, 2011

The Mechanize::Page::Link class is what you use to navigate from one page to another. The Link class itself is quite small, there just isn't much to a link. Primarily, to use a link you'll be searching for them with Mechanize::Page and "clicking" on them.

The Mechanize::Page::Link class itself just abstracts the anchor tag used to create links. In particular, the href and rel attributes and the link text itself.

Finding links within a page is covered in the Mechanize::Page section.

Clicking on Links

Once you have a Link object itself, clicking on it is just a matter of calling the Mechanize::Page::Link#click method. This method takes no arguments and will return a new Mechanize::Page object. The following example will open up the site reddit.com/ and click on the link with the text "programming" and print out all the article titles.


#!/usr/bin/env ruby
require 'rubygems'
require 'mechanize'

agent = Mechanize.new
page = agent.get('http://www.reddit.com/')
new_page = page.link_with(:text => 'programming').click

new_page.search(%Q{//div[@class='content']//p[@class='title']}).each do|link|
  puts link.inner_text
end

Other Things

In addition to clicking on links, there are a few methods to query information about the link.

  • attributes - The attributes method will return a Nokogiri::XML::Element for the link, which can be used like hash to query for any attributes needed. So you can do something like some_link.attributes["class"] to get the class attribute for any link.
  • href - Returns the href of the link, otherwise known as the URL the link points to.
  • node - Returns the same object as attributes. There are two methods for better readability in Mechanize code. When using attributes, it's clear you want to access an attribute, and it doesn't matter that you're really accessing a Nokogiri::XML::Element.
  • page - The Mechanize::Page that the link belongs to.
  • referer - Returns the same object as page.
  • rel - Returns an array of words in the rel attribute, or an empty list.
  • rel? - Takes a single argument, returns true if the array returned by rel contains that argument. A common use might be some_link.rel?('nofollow').
  • uri - Returns the result of URI.parse(href). This can be used to further query the URI object for more information, such as protocol, port, server or path.
  1. About.com
  2. Computing
  3. Ruby
  4. Tutorials
  5. The Mechanize 2.0 Handbook
  6. The Link

©2014 About.com. All rights reserved.