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. See GlobalForm for more methods.
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']
fields | -> | elements |
pretty_inspect | -> | inspect |
action | [RW] | |
buttons | [R] | |
checkboxes | [R] | |
enctype | [RW] | |
fields | [R] | |
file_uploads | [R] | |
form_node | [R] | |
method | [RW] | |
name | [RW] | |
page | [R] | |
radiobuttons | [R] |
Fetch the value of the first input field with the name passed in
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
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.
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.
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")
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 field 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' } )