http://www.perlmonks.org?node_id=500465

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/Mechanize.pm 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?

Thanks!

  • 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(); } }