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

Submit HTML Forms with WWW:Mechanize

by stanislav5000 (Novice)
on Mar 28, 2007 at 18:27 UTC ( [id://607080]=perlquestion: print w/replies, xml ) Need Help??

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

I would like to use WWW::Mechanize to look for forms that contain certain fields and automatically populate them with predetermined information. The catch is that I don't know PRECISELY what the field names will be. I want to fill out things that roughly match "FirstName, fname, Fname, etc" and was wondering if this is a good place to use regular expressions. Here is the code I'm using:
#!/usr/bin/perl use WWW::Mechanize ; #Prepopulated Information my $fname="Test"; my $lname="User"; my $address="1234 Example Street"; my $city="New York"; my $state="New York"; my $zip="10010"; my $phone="2135555555"; #Create new WWW::Mechanize object my $mech = WWW::Mechanize->new ; #Fetch URL or Die Tryin' $mech ->get("http://your.url/") ; die $mech ->res->status_line unless $mech ->success ; #Return list of forms found in the last fetched page to hand off to HT +ML::Form my @webforms = $mech->forms(); #Examine each form foreach my $form (@webforms) { my @inputfields = $form->param; #Examine each input field foreach my $inputfield (@inputfields) { if($inputfield =~ /(F|f)(irst)?name/) { $mech->set_fields( $inputfield => $fname); } if($inputfield =~ /(L|l(ast)?name/) { $mech->set_fields( $inputfield => $lname); } if($inputfield =~ /(A|a)ddress[123]?/) { $mech->set_fields( $inputfield => $address); } if($inputfield =~ /(C|c)ity//) { $mech->set_fields( $inputfield => $city); } if($inputfield =~ /(S|s)tate//) { $mech->set_fields( $inputfield => $state); } if($inputfield =~ /(Z|z)ip//) { $mech->set_fields( $inputfield => $zip); } if($inputfield =~ /(P|p)hone//) { $mech->set_fields( $inputfield => $phone); } # Submit Completed Form or Die Tryin' $mech ->submit ; die $mech ->res->status_line unless $mech ->success ; # If the form sends you somewhere, you can catch it : my $new_url = $mech ->response->request->uri->as_string ; } }
Thanks!

Replies are listed 'Best First'.
Re: Submit HTML Forms with WWW:Mechanize
by Fletch (Bishop) on Mar 28, 2007 at 19:26 UTC

    Hrmm . . .

    • you want to flexibly fill in personal information in arbitrary forms using predetermined information
    • most of the time one would be doing this would be to create accounts somewhere
    • most modern web browsers provide this functionality themselves, so J Random User really doesn't have a reason to need to automate this themself
    • spammers and other nefarious individuals would however be attempting to automatically sign up for large numbers of accounts

    You've just created your account today and have no prior history here so there's no existing reputation to go on, but this really sets the spammer-sense tingling. Then again perhaps you can provide a plausible explanation as to what you're trying to do that doesn't reek of naughtiness . . .

      My specific need is to perform input validation with the "predetermined information" across a bank of sanctioned sites. As you may have guessed, the predetermined text will be quite different. :) If that makes you warm and fuzzy, please kick in a little guidance. If not, thanks for the look.
Re: Submit HTML Forms with WWW:Mechanize
by runrig (Abbot) on Mar 28, 2007 at 19:27 UTC
    You can use the "forms()" method to get the list of forms, then use the HTML::Form api to get the list of input fields for the desired form, then match field names to what you're looking for.
      Do you mean something like this:
      #!/usr/bin/perl use WWW::Mechanize ; #Prepopulated Information my $fname="Test"; my $lname="User"; my $address="1234 Example Street"; my $city="New York"; my $state="New York"; my $zip="10010"; my $mech = WWW::Mechanize->new ; $mech ->get("http://your.url/") ; die $mech ->res->status_line unless $mech ->success ; #Hand off to HTML::Form my @webforms = $mech->forms(); foreach my $form (@webforms) { my @inputfields = $form->param; foreach my $inputfield (@inputfields) { if($inputfield =~ /(F|f)(irst)?name/) { $mech->set_fields( $inputfield => $fname); } if($inputfield =~ /(L|l(ast)?name/) { $mech->set_fields( $inputfield => $lname); } if($inputfield =~ /(A|a)ddress/) { $mech->set_fields( $inputfield => $address); } if($inputfield =~ /(C|c)ity//) { $mech->set_fields( $inputfield => $city); } if($inputfield =~ /(S|s)tate//) { $mech->set_fields( $inputfield => $state); } if($inputfield =~ /(Z|z)ip//) { $mech->set_fields( $inputfield => $zip); } if($inputfield =~ /(P|p)hone//) { $mech->set_fields( $inputfield => $phone); } # Submit $mech ->submit ; die $mech ->res->status_line unless $mech ->success ; # If the form sends you somewhere, you can catch it : my $url = $mech ->response->request->uri->as_string ; } }
      Let me know if I'm barking up the right tree.
        That's about what I was thinking. You've got extra slashes on the end of some of your regexes, and some can be simplified, e.g.
        /(S|s)tate/
        can be:
        /[Ss]tate/
        and if you don't care about case sensitivity at all, you can use:
        /(?i)state/
        or
        /state/i

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (3)
As of 2024-04-26 00:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found