bobf has asked for the wisdom of the Perl Monks concerning the following question:
I beseech the monks for wisdom and insight. I'm trying to learn how to use WWW::Mechanize to submit data to a form, but I'm clearly doing something wrong and try as I might I just can't seem to find the error. As a test case I'm using our beloved http://search.cpan.org and searching for 'mechanize'. Here's the code:
use strict; use warnings; use WWW::Mechanize; use HTTP::Cookies; use Data::Dumper; my $url = 'http://search.cpan.org'; my $searchstring = 'mechanize'; my $mech_obj = WWW::Mechanize->new(); $mech_obj->get( $url ); if( not $mech_obj->success() ) { print "Could not retrieve page:\n"; print $mech_obj->content(); die; } print "all forms:\n", Dumper( [ $mech_obj->forms ] ); $mech_obj->form_number( 1 ); $mech_obj->field( 'query' => $searchstring ); print "current form:\n", Dumper( $mech_obj->current_form() ); $mech_obj->submit(); print "content:\n", $mech_obj->content();
And here is the output:
all forms: $VAR1 = [ bless( { 'inputs' => [ bless( { 'value' => '', 'size' => '35', 'type' => 'text', 'name' => 'query' }, 'HTML::Form::TextInput' ), bless( { 'seen' => [ 1, 0, 0, 0 ], 'menu' => [ 'all', 'module', 'dist', 'author' ], 'current' => 0, 'type' => 'option', 'name' => 'mode' }, 'HTML::Form::ListInput' ), bless( { 'value' => 'CPAN Search', 'type' => 'submit' }, 'HTML::Form::SubmitInput' ) ], 'extra_attr' => { 'class' => 'searchbox', 'name' => 'f' }, 'enctype' => 'application/x-www-form-urlencoded', 'method' => 'GET', 'action' => bless( do{\(my $o = 'http://search.cpan +.org/search')}, 'URI::http' ) }, 'HTML::Form' ) ]; current form: $VAR1 = bless( { 'inputs' => [ bless( { 'value' => 'mechanize', 'size' => '35', 'type' => 'text', 'name' => 'query' }, 'HTML::Form::TextInput' ), bless( { 'seen' => [ 1, 0, 0, 0 ], 'menu' => [ 'all', 'module', 'dist', 'author' ], 'current' => 0, 'type' => 'option', 'name' => 'mode' }, 'HTML::Form::ListInput' ), bless( { 'value' => 'CPAN Search', 'type' => 'submit' }, 'HTML::Form::SubmitInput' ) ], 'extra_attr' => { 'class' => 'searchbox', 'name' => 'f' }, 'enctype' => 'application/x-www-form-urlencoded', 'method' => 'GET', 'action' => bless( do{\(my $o = 'http://search.cpan.o +rg/search')}, 'URI::http' ) }, 'HTML::Form' ); Unexpected field value http://search.cpan.org at (eval 5) line 1
Based on the output I know the proper form was selected and the query string was assigned to the proper field. I don't understand why I get an "Unexpected field value" error when the form is submitted, but I know there has to be a simple explanation for this.
Could someone please run my code and see if it works on a different system? That would at least tell me if the problem is in my code or if it is something else (e.g., the WWW::Mech installation, etc).
Thanks in advance!
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: "Unexpected field value" error with WWW::Mechanize
by jbrugger (Parson) on Mar 17, 2005 at 19:04 UTC | |
by bobf (Monsignor) on Mar 18, 2005 at 03:08 UTC | |
by jbrugger (Parson) on Mar 18, 2005 at 08:58 UTC | |
by Anonymous Monk on Jun 10, 2005 at 21:45 UTC | |
by tphyahoo (Vicar) on Mar 18, 2005 at 09:40 UTC | |
Re: "Unexpected field value" error with WWW::Mechanize - SOLVED
by bobf (Monsignor) on Jun 13, 2005 at 19:00 UTC |