Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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!


In reply to "Unexpected field value" error with WWW::Mechanize by bobf

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2024-04-23 22:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found