Search code examples
perllwplwp-useragent

What is my problem filling forms with LWP and HTTP::Request::Form?


I'm new to Perl, currently writing a Perl script to automatically fill web forms and submit them using LWP. The website URL is ***/something.cgi and in that document there is a form I need to fill, then hit submit. That takes me to another page which has another form to fill, but the website's URL remains the same.

I managed to fill the first form and submit it using:

$res = $ua->request($f->press("submit"));

where

my $f = HTTP::Request::Form->new($forms[0], $url);

Viewing $res->as_string shows the next page source, but tried to get the new forms in order to fill it, but it gave me the same form I already have. How can I get next page in order to fill its forms and proceed?


Solution

  • I would recommend you look at WWW::Mechanize and its form methods which is a subclass of LWP::UserAgent.

    EDIT

    Adding an example closely based on the example from my first link:

    use strict;
    use warnings;
    
    use WWW::Mechanize;
    my $mech = WWW::Mechanize->new();
    
    $mech->get( 'http://google.com' );
    sleep 1; ## be nice
    
    $mech->submit_form(
        form_number => 0,
        fields      => {
            q       => 'mungo',
        }
    );
    
    print $mech->content;