Class Mechanize::Form
In: lib/mechanize/form.rb
Parent: Object

This class encapsulates a form parsed out of an HTML page. Each type of input fields available in a form can be accessed through this object.

Examples

Find a form and print out its fields

  form = page.forms.first # => Mechanize::Form
  form.fields.each { |f| puts f.name }

Set the input field ‘name’ to "Aaron"

  form['name'] = 'Aaron'
  puts form['name']

Methods

External Aliases

fields -> elements
pretty_inspect -> inspect

Attributes

action  [RW] 
buttons  [R] 
checkboxes  [R] 
encoding  [RW]  Character encoding of form data (i.e. UTF-8)
enctype  [RW]  Content-Type for form data (i.e. application/x-www-form-urlencoded)
fields  [R] 
file_uploads  [R] 
form_node  [R] 
ignore_encoding_error  [RW]  When true, character encoding errors will never be never raised on form submission. Default is false
method  [RW] 
name  [RW] 
page  [R] 
radiobuttons  [R] 

Public Class methods

Public Instance methods

Fetch the value of the first input field with the name passed in

Example

Fetch the value set in the input field ‘name‘

 puts form['name']

Set the value of the first input field with the name passed in

Example

Set the value in the input field ‘name’ to "Aaron"

 form['name'] = 'Aaron'

This method adds a button to the query. If the form needs to be submitted with multiple buttons, pass each button to this method.

Add a field with field_name and value

This method builds an array of arrays that represent the query parameters to be used with this form. The return value can then be used to create a query string for this form.

Submit form using button. Defaults to the first button.

Removes all fields with name field_name.

This method is a shortcut to get form‘s DOM class. Common usage:

  page.form_with(:dom_class => "foorm")

Note that you can also use +:class+ to get to this method:

  page.form_with(:class => "foorm")

This method is a shortcut to get form‘s DOM id. Common usage:

  page.form_with(:dom_id => "foorm")

Note that you can also use +:id+ to get to this method:

  page.form_with(:id => "foorm")

Returns whether or not the form contains a field with field_name

has_key?(field_name)

Alias for has_field?

Treat form fields like accessors.

This method calculates the request data to be sent back to the server for this form, depending on if this is a regular post, get, or a multi-part post,

This method sets multiple fields on the form. It takes a list of fields which are name, value pairs.

If there is more than one field found with the same name, this method will set the first one found. If you want to set the value of a duplicate field, use a value which is a Hash with the key as the index in to the form. The index is zero based.

For example, to set the second field named ‘foo’, you could do the following:

  form.set_fields :foo => { 1 => 'bar' }

Submit this form with the button passed in

[Validate]