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

Hi. This may be fairly simple, but I do not see an obvious answer by looking through WWW::Mechanize's docs. I have a page that has 4 submit buttons, all with the same name, and I need to click on the 3rd one. I got it to work by muddling with the internal variables WWW::Mechanize uses, but I thought maybe there was a more friendly way to accomplish this.

Here's the working (albeit somewhat rude&dangerous) code:

   my $form = $agent->current_form();
   $agent->{req} = $form->find_input("list_PagingMove", undef, 3)->click($form);
   $agent->{res} = $agent->_do_request();

I know I could simply use $agent->click("list_PagingMove"), except it clicks on the first button of that name that it finds, not the 3rd like I need.

Replies are listed 'Best First'.
Re: using WWW::Mechanize with more than 1 submit button
by Corion (Patriarch) on Mar 01, 2003 at 18:05 UTC

    The buttons must be in different forms of that page, otherwise it makes no difference to the receiving CGI. So select the right form() and then click() on the button.

    For interactive exploration of a website, there is also WWW::Mechanize::Shell (written by me), which allows you to play with WWW::Mechanize in a more interactive manner and afterwards dump your session as Perl code.

    perl -MHTTP::Daemon -MHTTP::Response -MLWP::Simple -e ' ; # The $d = new HTTP::Daemon and fork and getprint $d->url and exit;#spider ($c = $d->accept())->get_request(); $c->send_response( new #in the HTTP::Response(200,$_,$_,qq(Just another Perl hacker\n))); ' # web
      The buttons must be in different forms of that page, otherwise it makes no difference to the receiving CGI.

      Yes it can, since they can have different values. This is perfectly legal.

      <form action="foo.html" method="get"> <p> <input type="hidden" name="foo" value="bar" /> <input type="submit" name="where" value="next" /> <input type="submit" name="where" value="prev" /> </p> </form>

      The value of where is depends on the submit button that is pressed.

      To answer the original question - it does seem to be a lack in the current W::M API - probably worth dropping a line to the author. He seems a friendly chap :-)

        Indeed - that's a difference I didn't notice (or banished from my mind). That page won't work with IE then, as IE (in my recollection) always sends the NAME of the button instead of the value (what an idiot move, who wants to receive the REPRESENTATION back ...). With Mozillla that page should work though. Or that page does parse the representation as well.

        perl -MHTTP::Daemon -MHTTP::Response -MLWP::Simple -e ' ; # The $d = new HTTP::Daemon and fork and getprint $d->url and exit;#spider ($c = $d->accept())->get_request(); $c->send_response( new #in the HTTP::Response(200,$_,$_,qq(Just another Perl hacker\n))); ' # web
Re: using WWW::Mechanize with more than 1 submit button
by bbfu (Curate) on Mar 01, 2003 at 16:42 UTC

    Disclaimer: I'm not all that familiar with WWW::Mechanize, so I may be missing something, but...

    Are all four submit buttons part of the same form? If not, then you can probably just use $agent->form(3) to select the form that contains the appropriate button and then click() it.

    If you have three buttons with the same name in the same form, then I don't think there's support for that in WWW::Mechanize (again, unless I'm missing something). It might be worth creating a patch or request and submitting it to the modules maintainer.

    bbfu
    Black flowers blossum
    Fearless on my breath

Re: using WWW::Mechanize with more than 1 submit button
by revdiablo (Prior) on Mar 02, 2003 at 00:31 UTC

    Thanks for the replies. Just to give you guys some closure -- I know you're dying to find out how I solved this ;) -- I sent the author an email. I included a link to this discussion in case he's interested.

    Oh, and yeah. All four buttons are in the same form. adrianh's reply is spot on, except there's a First and Last button along with the Previous and Next.

Re: using WWW::Mechanize with more than 1 submit button
by Anonymous Monk on Jul 07, 2003 at 11:47 UTC
    Hi, Is this (your code) work?
    my $form = $agent->current_form(); $agent->{req} = $form->find_input("list_PagingMove", undef, 3)->cli +ck($form); $agent->{res} = $agent->_do_request();
    > I tried this code, but for me its getting Internal Server Error. Error as ...
    Can't locate object method "_do_request" via package "WWW::Mechanize" +at /usr/fweb/mydomain/cgi-bin/test.pl line 37. Line 37 : - $agent->{res} = $agent->_do_request();
    If you can find out the error, pls post a comment here. Thanks

    20030707 Edit by Corion: Fixed formatting

      In the current versions of WWW::Mechanize, the _do_request method (which was considered private anyway) has been superseeded by the request() method. So most likely you will be able to replace that line by

      $agent->{res} = $agent->request($agent->{req});

      Note that I did not test this, as I don't have access to WWW::Mechanize in a testable form here.

      perl -MHTTP::Daemon -MHTTP::Response -MLWP::Simple -e ' ; # The $d = new HTTP::Daemon and fork and getprint $d->url and exit;#spider ($c = $d->accept())->get_request(); $c->send_response( new #in the HTTP::Response(200,$_,$_,qq(Just another Perl hacker\n))); ' # web

      As Corion rightly pointed out, the code in my original post used WWW::Mechanize in a non-standard fashion (you'll notice I made note of this in the original post as well). When I wrote the code, I was working around a problem with the interface -- hopefully that problem has now been corrected.

      I haven't touched this code in a while, but I might reassess to see if I can use it in a more polite manner. You should try using WWW::Mechanize the way it is described in its perldocs; it might just work. :^)