Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

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/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
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?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (6)
As of 2015-07-02 00:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (25 votes), past polls