Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

checking if form field exists in WWW::Mechanize

by Anonymous Monk
on Oct 15, 2005 at 17:20 UTC ( #500465=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I have a crawler that works on a dynamic web site. Sometimes certain parts of forms aren't available which causes my script to come to a screeching hault and tell me "No such field 'so and so' at /usr/lib/perl5/site_perl/5.8.4/WWW/ line 1151

How can I check to see if this form is here with WWW::Mech? I assume I could always parse the HTML to see if the form field exists but that's just ugly. Is there a way to do it inside the module?


  • Comment on checking if form field exists in WWW::Mechanize

Replies are listed 'Best First'.
Re: checking if form field exists in WWW::Mechanize
by johnnywang (Priest) on Oct 15, 2005 at 17:52 UTC
    WWW::Mechanize's field() function calls HTML::Form's value() function which croaks when the field doesn't exist. Without changing WWW::Mechanize (HTML::Form has a find_field function), you can always wrap your call in eval:
    eval{ $agent->field('field_name', 'foo') }; if($@){ if($@ =~ /No such field/i){ print "Field missing: field_name\n"; }else{ print "Something else is wrong\n"; } }
Re: checking if form field exists in WWW::Mechanize
by cazz (Pilgrim) on Oct 17, 2005 at 16:13 UTC
    W::M is built on top of a number of modules. The form handling is done in HTML::Form. A call to forms() on the mech object will return a list of HTML::Form objects for all of the forms. From the HTML::Form object, we can check if a field exists with find_input(). Something akin to this:
    foreach my $form ($mech->forms()) { if ($form->find_input("fieldname") { do_stuff(); } }

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://500465]
Approved by sk
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (5)
As of 2022-11-29 01:08 GMT
Find Nodes?
    Voting Booth?