Skip Menu |

Preferred bug tracker

Please visit the preferred bug tracker to report your issue.

This queue is for tickets about the WWW-Mechanize CPAN distribution.

Report information
The Basics
Id: 18399
Status: resolved
Priority: 0/
Queue: WWW-Mechanize

People
Owner: Nobody in particular
Requestors: jarich [...] perltraining.com.au
Cc:
AdminCc:

Bug Information
Severity: Normal
Broken in: 1.12
Fixed in: (no value)



Subject: value doesn't support multiple checkboxes with the same name.
I appear to be having difficulty attaching files, so I'll include some cut and paste here as well. I'm attempting to walk through a tick every checkbox with a given name. I don't care what the value is, I want to tick all of them. I thought I'd start by getting the value, so that I could pass the name and value to tick. The HTML is pretty straight forward: <form method="POST" action="test.cgi"> <input type="checkbox" name="check" value="1111" >1111 <input type="checkbox" name="check" value="2222" >2222 <input type="checkbox" name="check" value="3333" >3333 <input type="checkbox" name="check" value="4444" >4444 <input type="checkbox" name="check" value="5555" >5555 </form> And the code is as well: #!/usr/bin/perl -w use strict; use WWW::Mechanize; use Data::Dumper; my $mech = WWW::Mechanize->new(); $mech->get("http://localhost/~jarich/abc.html"); print Dumper $mech->forms(); print scalar $mech->value("check", 1); Unfortunately for all field numbers I pass to value I get a undefined value, when I expected to receive "1111" or "2222". Interestingly, if I edit the HTML to say that the field is "checked" then value returns the expected value "1111" etc. The data dump for each unchecked data dump looks like: bless( { 'current' => 0, 'menu' => [ { 'seen' => 1, 'value' => undef, 'name' => 'off' }, { 'value' => '1111', 'name' => '1111' } ], 'name' => 'check', 'type' => 'checkbox' }, 'HTML::Form::ListInput' ), If the checkbox is checked however it looks like: bless( { 'current' => 1, 'menu' => [ { 'value' => undef, 'name' => 'off' }, { 'seen' => 1, 'value' => '3333', 'name' => '3333' } ], 'name' => 'check', 'type' => 'checkbox' }, 'HTML::Form::ListInput' ), where the value of current and the hash with the "seen" value appears to map to whether the checkbox is checked upon loading. I'm hoping a work around will be to walk through the structure and set current => 1 for each element with the name "check".
Subject: test.pl
#!/usr/bin/perl -w use strict; use WWW::Mechanize; my $mech = WWW::Mechanize->new(); $mech->get("http://localhost/~jarich/abc.html"); use Data::Dumper; print Dumper $mech->forms(); print scalar $mech->value("check", 1);
On Tue Mar 28 02:01:36 2006, guest wrote: Show quoted text
> I'm hoping a work around will be to walk through the structure and set > current => 1 for each element with the name "check".
For the curious this work around seemed to do all I needed it to do. <pre> # Grab the first form out. my @forms = $mech->forms(); my $form = $forms[0]; # Check all the boxes foreach my $input ( @{$form->{inputs}} ) { next unless $input->{name} eq "check"; $input->{current} = 1; } </pre>
Show quoted text
> # Grab the first form out. > my @forms = $mech->forms(); > my $form = $forms[0]; > > # Check all the boxes > foreach my $input ( @{$form->{inputs}} ) { > next unless $input->{name} eq "check"; > > $input->{current} = 1; > }
A better solution is to use the HTML::Form interface: # Grab the first form out. my ($form) = $mech->forms; # Check all the boxes foreach my $input ($form->inputs) { my $name = $input->name || next; next unless $input->type eq 'checkbox'; next unless $name eq "check"; $input->check; } I've been thinking about whether this is a problem with value() not working as expected, or whether this is a problem with the word value having two meanings. On one hand it makes sense for the value() method to return undef if the checkbox is unchecked, as that's what will will be passed through to the web server when the form is submitted. On the other hand, in the HTML the keyword value is used to specify the value that would be passed through if the check box was checked. value() appears to act as the former suggests; returning undef for all unchecked check boxes and returning the value for all checked check boxes. Unfortunately I'm still unclear as to how (using Mech) to get the value I need to pass into tick(). I think essentially that this is really a documentation bug regarding the purpose of value() (and maybe regarding the use of tick) than a code bug. Assuming that value is supposed to behave the way it does. ;)