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

I have a site I parse using this fantastic module. However, the form I am using is not labled so I am forced to use the form number. The problem that is arising now is, they have a form that RANDOMLY appears some time (an advertisement) which throws my number off.

Is there a way to make it automagically find the form with a given field (say the field "shopwizard")?

Replies are listed 'Best First'.
Re: how smart www::mechanize can be?
by davidrw (Prior) on Mar 28, 2006 at 19:30 UTC
    y.. WWW::Mechanize uses HTML::Form underneath which has a find_input method..
    my $form = $mech->current_form; warn $form->find_input( 'shopwizard' );
    Update: added loop-age:
    my $form; foreach ( $mech->forms ){ if( $_->find_input('shopwizard') ){ $form = $_; last; } } # OR (yet another way; slightly less efficient): my @shopwizardForms = grep { $_->find_input( 'shopwizard' ) } $mech->f +orms; # or just: my ($shopwizardForm) = grep { $_->find_input( 'shopwizard' ) } $mech-> +forms;
      Hi. I tried
      my $form; foreach ( $mech->forms ){ if( $_->find_input( 'shopwizard' )){ $form = $_; last; } } print "---------------------- form was $form"; $mech->submit_form( form_number => $form, fields => { shopwizard => "Winter Love Usuki Set", criteria => 'exact' } );
      and it's erroring out now. When I print the $form, it prints HTML::Form=HASH(..). So if the $form has this, could this mean it's not passing the right information to my form?
        my $forms = $mech->forms(); my $form_num; for (0..$#$forms) { if ($forms->[$_]->find_input('shopwizard')) { $form_num = $_ + 1; last; } } die("Unable to find shopwizard form\n") unless $form_num; $mech->submit_form( form_number => $form_num, fields => { shopwizard => "Winter Love Usuki Set", criteria => 'exact' } );