in reply to Submit HTML Forms with WWW:Mechanize

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.
  • Comment on Re: Submit HTML Forms with WWW:Mechanize

Replies are listed 'Best First'.
Re^2: Submit HTML Forms with WWW:Mechanize
by stanislav5000 (Novice) on Mar 28, 2007 at 20:27 UTC
    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