I am having trouble with my loop. The intent is to: 1) Fetch a page 2) Look at forms for input fields 3) Look at input fields for regex matche3s 4) Submit only those forms that match the regex. I am receiving the following output:
X:\Scripts>perl formspider.pl Hit form loop on form:1 Hit inputfield loop Setting submit=0 Setting submit=0 Setting submit=0 Setting submit=0 Hit inputfield loop Setting submit=0 Setting submit=0 Hit inputfield loop Setting submit=0 Setting submit=0 Setting submit=0 No such field 'firstname' at X:/Perl/site/lib/WWW/Mechanize.pm line 57 +1
So it doesn't look like it's incrementing $formcount, and it is not submitting the right form. Here's my code:
#!/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"; my $submit="1"; #Create new WWW::Mechanize object my $mech = WWW::Mechanize->new ; #Fetch URL or Die Tryin' $mech ->get("http://www.example.com/") ; 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 my $formcount=1; print "Hit form loop on form:$formcount\n"; foreach my $form (@webforms) { my $submit=1; my @inputfields = $form->param; #Examine each input field print "Hit inputfield loop\n"; foreach my $inputfield (@inputfields) { if($inputfield =~ m/(F|f)(irst)?name/) { $mech->set_fields( $inputfield => $fname); print "matched firstname regex\n"; } 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); } else { $submit="0"; print "Setting submit=0\n"; } # Submit Completed Form or Die Tryin' if($submit=="1") { print "submitting request to form:$formcount\n"; $mech ->submit_form($formcount) ; 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 ; } } $formcount++; }
Help!?! Thanks!

In reply to WWW:Mechanize problems by stanislav5000

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.