WWW-Mechanize - Find a form based on the action url?

Posted on Tue Nov 7 02:17:23 2006 by moronic1
Find a form based on the action url?
Is it possible to find a form based on the action url in the form?

I haven't managed to find a way to do so in Mechanize.

I have a web page with multiple forms. None of the forms have a name and all of the forms have the exact same fields. The only difference is the url for the action of the forms.

I was hoping for something like find_form( action_regex => qr/my_action/ ) but I couldn't find anything like it in 1.20.

Perhaps something like this could be added:

=head2 $mech->find_form() Selects a form by regex on the action url. If there is more than one form on the page that matches the regex, then the first one is used. If it is found, the form is returned as an L<HTML::Form> object and set internally for later used with Mech's form methods such as C<L<field()>> and C<L<click()>>. Returns undef if no form is found. Note that this functionality requires libwww-perl 5.69 or higher. =cut sub find_form { my $self = shift; my %parms = ( n=>1, @_ ); $self->_clean_keys( \%parms, qr/^(n|(action|method|name)(_regex)?)$/ ); my @forms = $self->forms or return; my $nmatches = 0; my @matches; for my $form ( @forms ) { if ( _match_any_form_parms($form,\%parms) ) { ++$nmatches; return $form if $nmatches >= $parms{n}; } } return undef; } # find_form # Used by find_form to check for matches # The logic is such that ALL parm criteria that are given must match sub _match_any_form_parms { my $link = shift; my $p = shift; # No conditions, anything matches return 1 unless keys %$p; return if defined $p->{action} && !($form->action eq $p->{action} ); return if defined $p->{action_regex} && !($form->action =~ $p->{action_regex} ); return if defined $p->{method} && !(defined($form->method) && ($form->method eq $p->{met +hod} ); return if defined $p->{method_regex} && !(defined($form->method) && ($form->method =~ $p->{met +hod_regex} ); return if defined $p->{name} && !(defined($form->name) && $form->name eq $p->{name} ); return if defined $p->{name_regex} && !(defined($form->name) && $form->name =~ $p->{name_reg +ex} ); # Success: everything that was defined passed. return 1; }
'method_regex' is probably pretty useless though... :-)

Cheers
Direct Responses: 3878 | Write a response