Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

WWW::Mechanize and unnamed images

by Anonymous Monk
on Mar 02, 2006 at 04:25 UTC ( [id://533806]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to use WWW::Mechanize to submit a form which uses an image as a submit button without name. I am using the script below but no luck. Any advice? Thanks.
Here is the form <form method=get name=form1 action="http://address . . . <input type="image" alt=" Go " src="http://address/image.gif" border=0> <a href="http://address"> <img src="http://whatever/f-clear0.gif" border=0 alt="Clear"></a> </form> Here is the code: use WWW::Mechanize; my $agent = WWW::Mechanize->new(); $agent->get('http://address'); $agent->form(1); $agent->field(qw(request test)); #tried this but it didn't work #$agent->find_image(alt => " Go "); #$agent->click(); $agent->submit("Go"); print $agent->{content};

Replies are listed 'Best First'.
Re: WWW::Mechanize and unnamed images
by jbrugger (Parson) on Mar 02, 2006 at 05:24 UTC
    When you just want to submit a form, use:
    # Submits the page, without specifying a button to click. Actually, no + button is clicked at all. # This used to be a synonym for $mech->click("submit"), but is no long +er so. $mech->submit()
    If you want to click a button that isn't named, try to use the
    $mech->click_button( ... ) # number => n #Clicks the nth button in the current form. Numbering starts at 1. # value => value # Clicks the button with the value value in the current form.
    "We all agree on the necessity of compromise. We just can't agree on when it's necessary to compromise." - Larry Wall.
Re: WWW::Mechanize and unnamed images
by monarch (Priest) on Mar 02, 2006 at 05:18 UTC
    You need to learn more about HTML (check out HTML Help which is a great page about HTML tags).

    Looking at your HTML it is clear that your form has an action (<form .. action=".." ..>). That action specifies what web page WWW::Mechanize should fetch when you submit the form.

    Now looking at your image (<input type="image"...) it is clear that this image is not designed to actually perform any function by itself. It is likely that a javascript function elsewhere has tied itself to this image to make it clickable. Whatever that may be, the important information is already encoded in the form (the action as I mentioned earlier).

    So just try this code instead while paying particular attention to the <form ...> tag:

    use WWW::Mechanize; use strict; my $agent = WWW::Mechanize->new(); $agent->get('http://address'); $agent->form_name('form1'); $agent->field('request', 'test'); $agent->submit(); # will go to the form's action URL print( $agent->response->content() );
      Now looking at your image (<input type="image"...) it is clear that this image is not designed to actually perform any function by itself. It is likely that a javascript function elsewhere has tied itself to this image to make it clickable.
      No, <input type="image"> behaves the same as <input type="submit">, just instead of being a plain button w/text, it's whatever image you specify. So it does perform a function (submit form) by itself -- no javascript is involved. Note it also submits the coordinates of the image that were clicked, too.

      The HTML Help page is here: INPUT - Form Input (search for "graphical submit" to get to the right place in the page)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://533806]
Approved by kvale
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (5)
As of 2024-09-15 01:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    The PerlMonks site front end has:





    Results (21 votes). Check out past polls.

    Notices?
    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.