Class Mechanize
In: lib/mechanize/redirect_limit_reached_error.rb
lib/mechanize/util.rb
lib/mechanize/redirect_not_get_or_head_error.rb
lib/mechanize/form/image_button.rb
lib/mechanize/form/radio_button.rb
lib/mechanize/form/button.rb
lib/mechanize/form/option.rb
lib/mechanize/form/check_box.rb
lib/mechanize/form/multi_select_list.rb
lib/mechanize/form/select_list.rb
lib/mechanize/form/field.rb
lib/mechanize/form/file_upload.rb
lib/mechanize/cookie_jar.rb
lib/mechanize/unsupported_scheme_error.rb
lib/mechanize/file_saver.rb
lib/mechanize/page.rb
lib/mechanize/history.rb
lib/mechanize/page/image.rb
lib/mechanize/page/link.rb
lib/mechanize/page/base.rb
lib/mechanize/page/meta.rb
lib/mechanize/page/frame.rb
lib/mechanize/page/label.rb
lib/mechanize/content_type_error.rb
lib/mechanize/cookie.rb
lib/mechanize/response_code_error.rb
lib/mechanize/pluggable_parsers.rb
lib/mechanize/headers.rb
lib/mechanize/monkey_patch.rb
lib/mechanize/file.rb
lib/mechanize/chain.rb
lib/mechanize/chain/auth_headers.rb
lib/mechanize/chain/body_decoding_handler.rb
lib/mechanize/chain/connection_resolver.rb
lib/mechanize/chain/response_reader.rb
lib/mechanize/chain/request_resolver.rb
lib/mechanize/chain/response_header_handler.rb
lib/mechanize/chain/parameter_resolver.rb
lib/mechanize/chain/handler.rb
lib/mechanize/chain/ssl_resolver.rb
lib/mechanize/chain/custom_headers.rb
lib/mechanize/chain/header_resolver.rb
lib/mechanize/chain/uri_resolver.rb
lib/mechanize/chain/pre_connect_hook.rb
lib/mechanize/chain/response_body_parser.rb
lib/mechanize/file_response.rb
lib/mechanize/form.rb
lib/mechanize.rb
Parent: Object

Synopsis

The Mechanize library is used for automating interaction with a website. It can follow links, and submit forms. Form fields can be populated and submitted. A history of URL‘s is maintained and can be queried.

Example

 require 'rubygems'
 require 'mechanize'
 require 'logger'

 agent = Mechanize.new { |a| a.log = Logger.new("mech.log") }
 agent.user_agent_alias = 'Mac Safari'
 page = agent.get("http://www.google.com/")
 search_form = page.form_with(:name => "f")
 search_form.field_with(:name => "q").value = "Hello"
 search_results = agent.submit(search_form)
 puts search_results.body

Methods

Classes and Modules

Module Mechanize::Handler
Class Mechanize::Chain
Class Mechanize::ContentTypeError
Class Mechanize::Cookie
Class Mechanize::CookieJar
Class Mechanize::File
Class Mechanize::FileResponse
Class Mechanize::FileSaver
Class Mechanize::Form
Class Mechanize::Headers
Class Mechanize::History
Class Mechanize::Page
Class Mechanize::PluggableParser
Class Mechanize::RedirectLimitReachedError
Class Mechanize::RedirectNotGetOrHeadError
Class Mechanize::ResponseCodeError
Class Mechanize::UnsupportedSchemeError
Class Mechanize::Util

Constants

VERSION = '1.0.1.beta'   The version of Mechanize you are using.
AGENT_ALIASES = { 'Windows IE 6' => 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)', 'Windows IE 7' => 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)', 'Windows Mozilla' => 'Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.4b) Gecko/20030516 Mozilla Firebird/0.6', 'Mac Safari' => 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_2; de-at) AppleWebKit/531.21.8 (KHTML, like Gecko) Version/4.0.4 Safari/531.21.10', 'Mac FireFox' => 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; en-US; rv:1.9.2) Gecko/20100115 Firefox/3.6', 'Mac Mozilla' => 'Mozilla/5.0 (Macintosh; U; PPC Mac OS X Mach-O; en-US; rv:1.4a) Gecko/20030401', 'Linux Mozilla' => 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.4) Gecko/20030624', 'Linux Firefox' => 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.1) Gecko/20100122 firefox/3.6.1', 'Linux Konqueror' => 'Mozilla/5.0 (compatible; Konqueror/3; Linux)', 'iPhone' => 'Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en) AppleWebKit/420+ (KHTML, like Gecko) Version/3.0 Mobile/1C28 Safari/419.3', 'Mechanize' => "WWW-Mechanize/#{VERSION} (http://rubyforge.org/projects/mechanize/)"   User Agent aliases

External Aliases

redirect_ok -> follow_redirect?

Attributes

ca_file  [RW] 
cert  [RW] 
conditional_requests  [RW] 
cookie_jar  [RW] 
follow_meta_refresh  [RW] 
gzip_enabled  [RW] 
history  [R] 
history_added  [RW] 
html_parser  [RW]  The HTML parser to be used when parsing documents
html_parser  [RW] 
keep_alive  [RW] 
keep_alive_time  [RW] 
key  [RW] 
log  [RW] 
open_timeout  [RW] 
pass  [RW] 
pluggable_parser  [R] 
proxy_addr  [R]  Proxy settings
proxy_pass  [R] 
proxy_port  [R] 
proxy_user  [R] 
read_timeout  [RW] 
redirect_ok  [RW]  Controls how this agent deals with redirects. If it is set to true or :all, all 3xx redirects are automatically followed. This is the default behavior. If it is :permanent, only 301 (Moved Permanently) redirects are followed. If it is a false value, no redirects are followed.
redirection_limit  [RW] 
request_headers  [RW]  A hash of custom request headers
scheme_handlers  [RW] 
user_agent  [RW] 
verify_callback  [RW] 
watch_for_set  [RW] 

Public Class methods

Public Instance methods

Sets the user and password to be used for authentication.

Equivalent to the browser back button. Returns the most recent page visited.

basic_auth(user, password)

Alias for auth

If the parameter is a string, finds the button or link with the value of the string and clicks it. Otherwise, clicks the Mechanize::Page::Link object passed in. Returns the page fetched.

Returns a list of cookies stored in the cookie jar.

Returns the current page loaded by Mechanize

DELETE to url with query_params, and setting options:

  delete('http://tenderlovemaking.com/', {'q' => 'foo'}, :headers => {})

Fetch a file and return the contents of the file.

HEAD to url with query_params, and setting options:

  head('http://tenderlovemaking.com/', {'q' => 'foo'}, :headers => {})
page()

Alias for current_page

Posts to the given URL with the request entity. The request entity is specified by either a string, or a list of key-value pairs represented by a hash or an array of arrays.

Examples:

 agent.post('http://example.com/', "foo" => "bar")

 agent.post('http://example.com/', [ ["foo", "bar"] ])

 agent.post('http://example.com/', "<message>hello</message>", 'Content-Type' => 'application/xml')

PUT to url with entity, and setting options:

  put('http://tenderlovemaking.com/', 'new content', :headers => {'Content-Type' => 'text/plain'})

Sets the proxy address, port, user, and password addr should be a host, with no "http://"

Submit a form with an optional button. Without a button:

 page = agent.get('http://example.com')
 agent.submit(page.forms.first)

With a button

 agent.submit(page.forms.first, page.forms.first.buttons.first)

Runs given block, then resets the page history as it was before. self is given as a parameter to the block. Returns the value of the block.

Set the user agent for the Mechanize object. See AGENT_ALIASES

Returns whether or not a url has been visited

Returns a visited page for the url passed in, otherwise nil

[Validate]