Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

Submitting a second form

by rp132 (Novice)
on Jul 21, 2013 at 06:14 UTC ( #1045487=perlquestion: print w/replies, xml ) Need Help??
rp132 has asked for the wisdom of the Perl Monks concerning the following question:

I am running into a problem with gathering data from a website. When you first go to the website your only option is to enter an MAC address (ex. 1122.3344.5566) into a form. When you submit it the page returns with the same form as before but several other pieces of data formatted into several other forms as well.(uses same URL both before and after first form is submitted)

I can fill in the first form and submit it with no issue what so ever. The problem become when I get my results and then need to submit a form from what would be the second page. I can not figure out how to enter data on the second page which happens to be in the second form on page 2.

I can not share the actual code, but have placed generic information in place of such things as the actual URL.

use WWW::Mechanize; my $mech = WWW:Mechanize->new(); my $url = "" $mech->get(url); $result = $mech->submit_form( form_number => 1, fields => { modem => '1122.3344.5566' } ,button => 'submit' ); #### up to this point everything works perfectly. #### $result->content gives me all of the data I would #### normally see $results2 = $mech->submit_form( form_number => 2, fields => { modem => '1122.3344.5566' ip_select => '' } ); #### the submit button does not have a name on this form #### and if I read the docs correct the form will still #### submit using submit()

The error I get is: There is no form number 2. I am still pretty new to PERL and as such am stuck on how to fill in the second form and get it submitted. Any wisdom would be great. Thank you in advice.

Replies are listed 'Best First'.
Re: Submitting a second form
by Loops (Curate) on Jul 21, 2013 at 06:19 UTC

    Shot in the dark... have you tried to use "form_number => 1" for the second submission as well? Likely there is only one form on the second page.

      Yes I have tried changing form numbers and everything else I can think of. What appears to happen is that the original get() only pulls the first page which only has one form, with one input field and a submit button. The other forms and fields only show up after the first form is submitted. What I believe is the issue is that my results are just that results, but the script does not know or have a way to interact with the webpage after the first form is submitted.

      I have noticed that if I add the code for the radio button in the first submit that that radio button is checked in my results from the first submit however, it is not returning the results. Based on this I am assuming that I could just submit any form with those fields filled in and the site will except it and return the results. It appears that it may depend on the value of the submit button that is used. Is there a way to set a submit value especially if the submit button does not have a name? Below is the page code of the site:

      <form name="testform" action="test.tcl" method="POST"> <input> type="hidden" name "modem" value="1122.3344.5566"> <input> type="hidden" name="progName" value "test"> <input> type="hidden" name "called_program" value "/var/data/tools/cgi +-bin/test.tcl"> <input type="radio" name="ip_select" value=""<br> <input type="hidden" name="ip_select_type" value="xyz"> <input type="submit" value="gather this device from another">

      I excluded the other radio buttons as the is the only one I am interested in submitting

Re: Submitting a second form
by daxim (Chaplain) on Jul 21, 2013 at 13:44 UTC
    Counting forms is not necessary when the forms have unique characteristics, which in practice they always have.
    $mech->submit_form( with_fields => { modem => '1122.3344.5566', ip_select => '', } );

      I tried the example you gave and thought that it may work, but I get the error "There is no form with the requested fields".

        Hello, I would recommend getting the firefox add-on Firebug, inspect the page for the form's name in the HTML, and add it that way. for example:
        $mech->submit_form ( form_name => 'form', fields => { modem => '1122.3344.5566', ip_select => ''} );
        In fact, looking at your code again to give you that example, problems may have been caused by not separating the field names with a comma? But I'm not sure.. see if that works for you, though..
        Actually, it's not an error. There is no form with the requested fields. Is the url correct? Any syntax problems? Try this:
        #!/usr/bin/perl use strict; use warnings; use WWW::Mechanize; my $url = ''; my $mech = WWW::Mechanize->new( autocheck => 1 ); $mech->get($url); my(%fields) = ( modem => '1122.3344.5566', ip_select => '', ); my $result = $mech->submit_form( with_fields => %fields, ); print $result;

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1045487]
Approved by kcott
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (6)
As of 2018-06-18 11:21 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (109 votes). Check out past polls.