Aditya J. Aditya J. - 11 months ago 44
HTML Question

WWW::Mechanize: How To Click Button Based On Its Number in Form

There is a button on a website at the end of a form that I cannot seem to click with WWW::Mechanize. Here is the bit of HTML pertaining to this button:

<input type="submit" class="saveButton" value="Login">

When I
print $mech->find_all_inputs();
, I get return this:


The third one is the one I want to click. I'm not exactly sure how to click this button even though I've found it. I tried
click(field(n => 3))
, I tried assigning a variable $submit to
, then
, and no matter what, this button is not clicked.

Can anyone guide me as to how to click this elusive button?

Edit (after question answered)

Interrogating the HTML form found I was actually entering the password for the login into the 'Forgot my Password' field of the form. Why this field was not coming up for
, I don't know since "Login" was. Either way, clicking the button takes me to the next page. Thanks!

Answer Source

Did you try to select the appropriate form first, then call click? It says (my emphasis)

Has the effect of clicking a button on the current form.

Find which form on the page you need. Let's say it's form number 2.

# $ua is the User Agent (Mechanize object), at the appropriate page
# fill the form ...
my $response = $ua->click();


    form_number => 2, 
    # fields => { name => $value }  # can fill it here as well

I find click to be perhaps more reliable overall.

To inspect the forms on the page you can use my @forms = $ua->forms. To fill the form you can use select or set_fields, for example. Useful methods are value_names and possible_values. See Form Methods. All this operates with HTML::Form objects so you can use its methods.

If this doesn't help please give us more detail -- the web page in question would be ideal.