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

Hi all,

I had made an attempt to use WWW::Mechanize module using the following code.

In the below code, yes.html contains two field say UserId and Passwd and action for the form is login.cgi. If i use the following code , getting result a "blank" page. While looking into the error_log file, shows the error as
There is no form numbered 1 at /usr/pweb/phonekerala/cgi-bin/thisis.pl + line 26 Can't call method "value" on an undefined value at /usr/lib/perl5/site +_perl/5.6.0/WWW/Mechanize.pm line 409.

How can i get the display page using these parameters?

After running the program i should modify the code to integrate click function etc. So please help me to provide a solution to my above said problem.

Thanks

Code ..... Getting result page as blank.........
#!/usr/bin/perl print "Content-type: text/html\n\n"; use warnings; use strict; use WWW::Mechanize; use CGI; my $cgi = CGI->new(); my $form = $cgi->Vars; my $agent = WWW::Mechanize->new(); $agent->get('http:/mydomain.com/html/yes.html'); $agent->form_number('1'); $form->{UserId}='test01'; $form->{Passwd} = 's'; $agent->field('UserId',$form->{UserId}); $agent->field('Passwd',$form->{Passwd}); $agent->submit(); $agent->get('http://mydomain.com/cg-bin/login.cgi?UserId='.$form-> +{UserId}.'&Passwd='.$form->{Passwd}); print $agent->content();

20030704 Edit by Corion: Added formatting

Replies are listed 'Best First'.
Re: Getting result page using WWW::Mechanize
by Corion (Patriarch) on Jul 04, 2003 at 09:28 UTC

    Actually, you are getting two things here :

    The message There is no form numbered 1 at /usr/pweb/phonekerala/cgi-bin/thisis.pl line 26 is a warning caused by the code :

    $agent->form_number('1');

    WWW::Mechanize::Shell adds this code as a safety measure in case the first form is not automatically selected.

    The most likely reason why you get that warning though is, that the first call to get did not return a valid page, this is why you can't set the values in the second try. Check this by adding a line like

    print "Got the page ", $agent->content;
    or
    die $agent->res->as_string unless $agent->content;

    I will implement a die_on_http_failure for WWW::Mechanize in one of the upcoming releases that will make the whole script die horribly if any http request fails - but don't hold your breath for that :-)

    Mostly, this will implement the following idiom :

    die $agent->res->as_string unless $agent->res->is_success;
    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

      Hi Corion,

      Thanks for your help and . I made some changes like this and working fine... but still not getting the

      #!/usr/bin/perl print "Content-type: text/html\n\n"; use warnings; use strict; use WWW::Mechanize; use CGI; my $cgi = CGI->new(); my $form = $cgi->Vars; my $agent = WWW::Mechanize->new(); $agent->get('http:/mydomain.com/html/yes.html'); $agent->form_number('0'); ##changes made here $agent->form_name('test'); ## Added $form->{UserId}='test01'; $form->{Passwd} = 's'; $agent->field('UserId',$form->{UserId}); $agent->field('Passwd',$form->{Passwd}); $agent->submit(); ##$agent->get('http://mydomain.com/cg-bin/login.cgi?#UserId='.$for +m-> #+{UserId}.'&Passwd='.$form->{Passwd}); print $agent->content();

      Submitting this form will call a html that contains frame... in that frame set, one frame is calling a cgi .. it's ponting to the location where i am running the script... That's why i am geting error as "Page can't be found.

      I think u are the author of "WWW::Mechanize::Shell " module. Just now i go through that module... Excellent. So could you please tell me how to handle multiple files using your module... In my result page , i have a text field and submit button... so how can i input the parameter and submit that form?

      Please send me the valuable solution and suggestion to tackle these type of problems...

      Once again thanks for your help.
      Regards
      ps

      20030704 Edit by Corion: Fixed formatting

        If you have to navigate a page containing frames, you have to navigate first to the frame page, and then use

        $agent->follow();

        to get to the page linked from that frame.

        To set the value of a text field, use $agent->field("fieldname",$value);. To submit a form with a single button, you can either use $agent->submit() or $agent->click("button_name")

        BTW, take a look at the Writeup Formatting Tips to see how to format code and your text.

        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