Mechanize v0.1.0 Mechanize.Form View Source

Encapsulates all functionalities related to form handling and submission.

You can fetch a form from a page using Mechanize.Page module:

form = Page.form_with(page, name: "login")

Link to this section Summary

Types

t()

The HTML Form struct.

Functions

Check all checkboxes matching the given query.

Checks a radio button matching the given query.

Returns a list of checkboxes or an empty list if no checkboxes are found.

Returns a list of checkboxes matching the given query.

Clicks on a submit button matching the given query.

Clicks on a image input matching the given query.

Returns all fields from the given form.

Fill a text input with a given value.

Returns a list of image inputs or an empty list if no image input are found.

Returns a list of image inputs matching the given query.

Returns a list of radio buttons or an empty list if no radio buttons are found.

Returns a list of radio buttons matching the given query.

Selects an option from select list matching the given query.

Returns a list of selects or an empty list if no selects are found.

Returns a list of selects matching the given query.

Submits the given form.

Returns a list of submit buttons or an empty list if no submit buttons are found.

Returns a list of submit buttons matching the given query.

Returns a list of text inputs or an empty list if no text inputs are found.

Returns a list of text inputs matching the given query.

Uncheck all checkboxes matching the given query.

Unchecks a radio button matching the given query.

Unselects an option from select list matching the given query.

Link to this section Types

Specs

t() :: %Mechanize.Form{element: Mechanize.Page.Element.t(), fields: list()}

The HTML Form struct.

Link to this section Functions

Link to this function

check_checkbox(form, query)

View Source

Specs

check_checkbox(t(), Mechanize.Query.t()) :: t()

Check all checkboxes matching the given query.

Raises Mechanize.Query.BadQueryError if no checkbox is matched by the query.

See Mechanize.Query module documentation to know all query capabilities in depth.

Example

You can check a checkbox and submit the for after:

form
|> Form.check_checkbox(name: "subscribe", value: "yes")
|> Form.submit!()
Link to this function

check_radio_button(form, query)

View Source

Specs

check_radio_button(t(), Mechanize.Query.t()) :: t()

Checks a radio button matching the given query.

When you check a radio button, Mechanize does the job to uncheck all radios from the same radio group (i.e. same name attribute) before check the radio button in the query.

Raises Mechanize.Query.BadQueryError if no radio button is matched by query. Also raises if two or more radio buttons from the same radio group are checked by the query.

See Mechanize.Query module documentation to know all query capabilities in depth.

Example

Checks a radio button and submit the form:

form
|> Form.check_checkbox(name: "subscribe", value: "yes")
|> Form.submit!()

Specs

checkboxes(t()) :: [Mechanize.Form.Checkbox.t()]

Returns a list of checkboxes or an empty list if no checkboxes are found.

See related check_checkbox/2 and uncheck_checkbox/2.

Link to this function

checkboxes_with(form, query)

View Source

Specs

checkboxes_with(t(), Mechanize.Query.t()) :: [Mechanize.Form.Checkbox.t()]

Returns a list of checkboxes matching the given query.

An empty list is returned in case no checkbox is matched by the given query.

See related check_checkbox/2 and uncheck_checkbox/2.

Example

Returns all checkboxes with name "download".

Form.text_inputs(form, name: "download")
Link to this function

click_button!(form, query)

View Source

Specs

click_button!(t(), Mechanize.Query.t()) :: Page.t()

Clicks on a submit button matching the given query.

Mechanize submits the form when an submit button is clicked and a Mechanize.Page struct is returned as the result.

Raises Mechanize.Query.BadQueryError if none or more than one submit button is matched by query.

Raises additional exceptions from Mechanize.Browser.request!/5.

See Mechanize.Query module documentation to know all query capabilities in depth.

Example

You can click on an submit button by its visible text:

SubmitButton.click_button!(form, "OK")

You can also click by attribute name:

SubmitButton.click_button!(form, name: "submit1")

Fill a login form and submit by clicking in "OK" submit button:

form
|> Form.fill_text(name: "username", with: "me@example.com")
|> Form.fill_text(name: "password", with: "123456")
|> Form.click_button!("OK")
Link to this function

click_image!(form, query)

View Source

Specs

click_image!(t(), Mechanize.Query.t()) :: Page.t()

Clicks on a image input matching the given query.

Mechanize submits the form when an image input is clicked and a Mechanize.Page struct is returned as the result.

Raises Mechanize.Query.BadQueryError if none or more than one image input is matched by query.

Raises additional exceptions from Mechanize.Browser.request!/5.

See Mechanize.Query module documentation to know all query capabilities in depth.

Example

You can click on an image input:

Form.click_image!(form, name: "america")

You can also send x,y coordinates of the click:

Form.click_image!(form, name: "america", x: 120, y: 120)

Specs

fields(t()) :: list()

Returns all fields from the given form.

Specs

fill_text(t(), Mechanize.Query.t()) :: t()

Fill a text input with a given value.

Text inputs are all inputs that can store text, not just limited to inputs of the type="text". Mechanize treats color, date, datetime, email, hidden, month, number, password, range, search, tel, text, time, url, week and textarea as text inputs.

See Mechanize.Query module documentation to know all query capabilities in depth.

Example

You can fill a login form like this:

form
|> Form.fill_text(name: "username", with: "me@example.com")
|> Form.fill_text(name: "password", with: "123456")
|> Form.submit!()

Specs

image_inputs(t()) :: [Mechanize.Form.ImageInput.t()]

Returns a list of image inputs or an empty list if no image input are found.

See related click_image!/2.

Link to this function

image_inputs_with(form, query)

View Source

Specs

image_inputs_with(t(), Mechanize.Query.t()) :: [Mechanize.Form.ImageInput.t()]

Returns a list of image inputs matching the given query.

An empty list is returned in case no image input is matched by the given query.

See related click_image!/2.

Example

Returns all image inputs with name "america".

Form.image_inputs_with(form, name: "america")

Specs

radio_buttons(t()) :: [Mechanize.Form.RadioButton.t()]

Returns a list of radio buttons or an empty list if no radio buttons are found.

See related check_radio_button/2 and uncheck_radio_button/2.

Link to this function

radio_buttons_with(form, query)

View Source

Specs

radio_buttons_with(t(), Mechanize.Query.t()) :: [Mechanize.Form.RadioButton.t()]

Returns a list of radio buttons matching the given query.

An empty list is returned in case no radio button is matched by the given query.

See related check_radio_button/2 and uncheck_radio_button/2.

Example

Returns all radio buttons with name "subscribe".

Form.radio_buttons_with(form, name: "subscribe")

Specs

select(t(), Mechanize.Query.t()) :: t()

Selects an option from select list matching the given query.

In case of selects without multiple attribute, Mechanize does the job to unselect all options from the same select list before it selects the given option.

Raises Mechanize.Query.BadQueryError if no select or option is matched by query. Also raises when two or more options from the same select list are selected by the query and multiple attribute is not present.

See Mechanize.Query module documentation to know all query capabilities in depth.

Examples

Selects an option with text "Option 1" on a select with name="select1".

Form.select(form, name: "select1", option: "Option 1")

Select by value attribute:

Form.select(form, name: "select1", option: [value: "1"])

Or select the third option of a select (note that Mechanize uses a zero-based index):

Form.select(form, name: "select1", option: 2)

Specs

select_lists(t()) :: [Mechanize.Form.SelectList.t()]

Returns a list of selects or an empty list if no selects are found.

See related select/2 and unselect/2.

Link to this function

select_lists_with(form, query)

View Source

Specs

select_lists_with(t(), Mechanize.Query.t()) :: [Mechanize.Form.SelectList.t()]

Returns a list of selects matching the given query.

An empty list is returned in case no selects is matched by the given query.

See related select/2 and unselect/2.

Example

Returns all selects with name "category".

Form.select_lists_with(form, name: "category")
Link to this function

submit!(form, button \\ nil)

View Source

Specs

submit!(t(), Mechanize.Form.SubmitButton.t() | Mechanize.Form.ImageInput.t()) ::
  Page.t()

Submits the given form.

Mechanize submits the form and a Mechanize.Page struct is returned as the result.

To simulate a form submited by a button click, pass the button as the second parameter or use any of our helper functions click_button!/2 or click_image!/2. To simulate a form submited by enter key press, ignore the second parameter.

Raises additional exceptions from Mechanize.Browser.request!/5.

Example

Simulate a login form submission by pressing "enter":

form
|> Form.fill_text(name: "username", with: "me@example.com")
|> Form.fill_text(name: "password", with: "123456")
|> Form.submit!()

Simulate a login form submission by clicking the submit button:

button =
  form
  |> Form.submit_buttons()
  |> List.first()

form
|> Form.fill_text(name: "username", with: "me@example.com")
|> Form.fill_text(name: "password", with: "123456")
|> Form.submit!(button)

See click_button!/2 for a simpler way to do this.

Specs

submit_buttons(t()) :: [Mechanize.Form.SubmitButton.t()]

Returns a list of submit buttons or an empty list if no submit buttons are found.

See related select/2 and unselect/2.

Link to this function

submit_buttons_with(form, query)

View Source

Specs

submit_buttons_with(t(), Mechanize.Query.t()) :: [
  Mechanize.Form.SubmitButton.t()
]

Returns a list of submit buttons matching the given query.

An empty list is returned in case no submit button is matched by the given query.

See related click_button!/2.

Example

Returns all submit buttons with name "send".

Form.submit_buttons_with(form, name: "send")

Specs

text_inputs(t()) :: [Mechanize.Form.TextInput.t()]

Returns a list of text inputs or an empty list if no text inputs are found.

See related fill_text/2.

Link to this function

text_inputs_with(form, query)

View Source

Specs

text_inputs_with(t(), Mechanize.Query.t()) :: [Mechanize.Form.TextInput.t()]

Returns a list of text inputs matching the given query.

An empty list is returned in case no text input is matched by the given query.

See related fill_text/2.

Example

Returns all text inputs with name "download".

Form.text_inputs(form, name: "download")
Link to this function

uncheck_checkbox(form, query)

View Source

Specs

uncheck_checkbox(t(), Mechanize.Query.t()) :: t()

Uncheck all checkboxes matching the given query.

Raises Mechanize.Query.BadQueryError if no checkbox is matched by the query.

See Mechanize.Query module documentation to know all query capabilities in depth.

Example

You can uncheck a checkbox and submit the for after:

form
|> Form.uncheck_checkbox(name: "subscribe", value: "yes")
|> Form.submit!()
Link to this function

uncheck_radio_button(form, query)

View Source

Specs

uncheck_radio_button(t(), Mechanize.Query.t()) :: t()

Unchecks a radio button matching the given query.

Raises Mechanize.Query.BadQueryError if no radio button is matched by query.

See Mechanize.Query module documentation to know all query capabilities in depth.

Example

Unchecks a radio button and submit the form:

form
|> Form.uncheck_checkbox(name: "subscribe", value: "yes")
|> Form.submit!()

Specs

unselect(t(), Mechanize.Query.t()) :: t()

Unselects an option from select list matching the given query.

Raises Mechanize.Query.BadQueryError if no select or option is matched by query.

See Mechanize.Query module documentation to know all query capabilities in depth.

Examples

By option with text "Option 1" on a select with name="select1".

Form.select(form, name: "select1", option: "Option 1")

By value attribute:

Form.select(form, name: "select1", option: [value: "1"])

Or unselect the third option of a select (note that Mechanize uses a zero-based index):

Form.select(form, name: "select1", option: 2)