Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

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 -

Replies are listed 'Best First'.
Re: Problem submitting form with Mechanize
by marto (Archbishop) on Jan 05, 2011 at 22:59 UTC

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

      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/ line 1804.
      So where does that leave me now?

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://880704]
Approved by marto
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (7)
As of 2018-07-20 20:59 GMT
Find Nodes?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?

    Results (441 votes). Check out past polls.