Class: WWW::Mechanize

Class | WWW::Mechanize
In: | lib/www/mechanize.rb lib/www/mechanize/chain.rb lib/www/mechanize/chain/auth_headers.rb lib/www/mechanize/chain/body_decoding_handler.rb lib/www/mechanize/chain/connection_resolver.rb lib/www/mechanize/chain/custom_headers.rb lib/www/mechanize/chain/header_resolver.rb lib/www/mechanize/chain/parameter_resolver.rb lib/www/mechanize/chain/pre_connect_hook.rb lib/www/mechanize/chain/request_resolver.rb lib/www/mechanize/chain/response_body_parser.rb lib/www/mechanize/chain/response_header_handler.rb lib/www/mechanize/chain/response_reader.rb lib/www/mechanize/chain/ssl_resolver.rb lib/www/mechanize/chain/uri_resolver.rb lib/www/mechanize/content_type_error.rb lib/www/mechanize/cookie.rb lib/www/mechanize/cookie_jar.rb lib/www/mechanize/file.rb lib/www/mechanize/file_response.rb lib/www/mechanize/file_saver.rb lib/www/mechanize/form.rb lib/www/mechanize/form/button.rb lib/www/mechanize/form/check_box.rb lib/www/mechanize/form/field.rb lib/www/mechanize/form/file_upload.rb lib/www/mechanize/form/image_button.rb lib/www/mechanize/form/multi_select_list.rb lib/www/mechanize/form/option.rb lib/www/mechanize/form/radio_button.rb lib/www/mechanize/form/select_list.rb lib/www/mechanize/headers.rb lib/www/mechanize/history.rb lib/www/mechanize/monkey_patch.rb lib/www/mechanize/page.rb lib/www/mechanize/page/base.rb lib/www/mechanize/page/frame.rb lib/www/mechanize/page/link.rb lib/www/mechanize/page/meta.rb lib/www/mechanize/pluggable_parsers.rb lib/www/mechanize/redirect_limit_reached_error.rb lib/www/mechanize/redirect_not_get_or_head_error.rb lib/www/mechanize/response_code_error.rb lib/www/mechanize/unsupported_scheme_error.rb lib/www/mechanize/util.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 = WWW::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

auth back basic_auth click cookies current_page delete get get_file head log log= max_history max_history= new page post post_connect_hooks pre_connect_hooks put set_proxy submit transact user_agent_alias= visited? visited_page

### Classes and Modules

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

### Constants

VERSION | = | '0.9.3' |  | 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; PPC Mac OS X; en) AppleWebKit/418 (KHTML, like Gecko) Safari/417.9.3', 'Mac FireFox' => 'Mozilla/5.0 (Macintosh; U; PPC Mac OS X Mach-O; en-US; rv:1.8.0.3) Gecko/20060426 Firefox/1.5.0.3', '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 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] |
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] |
read_timeout | [RW] |
redirect_ok | [RW] |
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

new() {|self if block_given?| ...}

### Public Instance methods

auth(user, password)

Sets the user and password to be used for authentication.

back()

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

basic_auth(user, password)

Alias for auth

click(link)

Clicks the WWW::Mechanize::Link object passed in and returns the page fetched.

cookies()

Returns a list of cookies stored in the cookie jar.

current_page()

Returns the current page loaded by Mechanize

delete(url, query_params = {}, options = {})

DELETE to `url` with `query_params`, and setting `options`:

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

get(options, parameters = [], referer = nil) {|page if block_given?| ...}

Fetches the URL passed in and returns a page.

get_file(url)

Fetch a file and return the contents of the file.

head(url, query_params = {}, options = {}) {|page if block_given?| ...}

HEAD to `url` with `query_params`, and setting `options`:

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

log()
log=(l)
max_history()
max_history=(length)
page()

Alias for current_page

post(url, query={})

Posts to the given URL wht the query parameters passed in. Query parameters can be passed as a hash, or as an array of arrays. Example:

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

or

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

post_connect_hooks()
pre_connect_hooks()
put(url, query_params = {}, options = {})

PUT to `url` with `query_params`, and setting `options`:

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

set_proxy(addr, port, user = nil, pass = nil)

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

submit(form, button=nil, headers={})

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)

transact() {|self| ...}

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.

user_agent_alias=(al)

Set the user agent for the Mechanize object. See AGENT_ALIASES

visited?(url)

Returns whether or not a url has been visited

visited_page(url)

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

[Validate]