Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Problem submitting form with Mechanize

by pat_mc (Pilgrim)
on Jan 05, 2011 at 22:51 UTC ( #880704=perlquestion: print w/ replies, xml ) Need Help??
pat_mc has asked for the wisdom of the Perl Monks concerning the following question:

Esteemed Monks,

I am trying to scrape a site using WWW::Mechanize. Somehow, I am not managing to submit a very simply form containing radio buttons. I hope you can help! This is the form code from the page source:
<form method="post" action="http://..."> <div class="ABC"> <div class="DEF"><input type="radio" name="Radio" id="1" value="1" />< +label for="1">Option1</label></div> <div class="DEF"><input type="radio" name="Radio" id="2" value="2" />< +label for="2">Option2</label></div> <div class="DEF"><input type="radio" name="Radio" id="3" value="3" />< +label for="3">Option3</label></div> </div> <div class="buttons"> <div class="spacer"></div> <input type="submit" class="form_buttons" name="submit" value="Submit +Form" /> <div class="form_buttons"><a href="http://..." title="...">...</a></di +v> <input type="hidden" name="ID" value="Form_ID" /> <div class="spacer"></div> </div> </form>

And this is how I tried to submit the form:
#!/usr/bin/perl -w use strict; use WWW::Mechanize; use Crypt::SSLeay; use HTTP::Cookies; my $agent = WWW::Mechanize->new(); my $cookie_file = "Cookies.txt"; $agent -> cookie_jar( HTTP::Cookies->new( File => $cookie_file, autosa +ve => 1 )); my $page = 'http://...'; # Request the page. $agent -> get( $page ); # Set the radiobutton 'Radio' to the second option. $agent -> set_fields( 'Radio' => '2' ); # Now post the poll form to the URL given in the source code under act +ion="http://...". $agent -> post( 'http://...' );

Thanks for your help with this in advance!
Cheers -
Pat

Comment on Problem submitting form with Mechanize
Select or Download Code
Re: Problem submitting form with Mechanize
by marto (Bishop) on Jan 05, 2011 at 22:59 UTC

    You don't seem to be submitting the form, try:

    $agent->submit();
      Hm ... thanks, marto. The reason why I did not include a form submit was because of the line
      <form method="post" action="http://...">
      When I do include the submit, I receive the following error message:
      Can't call method "make_request" on an undefined value at C:/Perl/lib/ +WWW/Mechanize.pm line 1804.
      So where does that leave me now?

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (9)
As of 2014-12-27 20:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (177 votes), past polls