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

Hey there monks, I'm using mechanize on a site for a project, but the HTML is wrong so mechanize is (understandably) not working. I'm using the ->tick function on a checkbox, but the problem is they have it like this
<input type="checkbox" name="passenger" value="somevalue" />
It's good enough for firefox, but how can i get mechanize to work with this terribly written HTML (which i have no chance of getting changed), or how else can i accomplish the task? Thanks!

Replies are listed 'Best First'.
Re: WWW::Mechanize on broken HTML
by naikonta (Curate) on Jun 21, 2007 at 03:28 UTC
    What you mean terribly written HTML? I's a valid XHTML tag. I expected you put the code with WWW::Mechanize and pointed out which part didn't work as you expect. But OK, supposed we have a HTML file (form.html) with the following simplified content:
    <form> <input type="checkbox" name="passenger" value="somevalue" /> <input type="submit" name=".submit" value="OK"> </form>
    Update: (21-06-2007) Oops, fixed the submit field.

    The following example (tick.pl) demonstrates the checkbox ticking.

    use strict; use warnings; use WWW::Mechanize; # set some dummy vars my $target_field = 'passenger'; my $target_value = 'somevalue'; # get the URL and check my $url = shift or die "Need URL, pal!\n"; my $mech = WWW::Mechanize->new; $mech->get($url); $mech->success or die $mech->response->status_line, "\n"; # select the form $mech->form_number(1); # check original value my $set_value = $mech->value($target_field); $set_value = 'undef' unless defined $set_value; print "[1] value for $target_field: ($set_value)\n"; # tick the checkbox $mech->tick($target_field, $target_value); $set_value = $mech->value($target_field); $set_value = 'undef' unless defined $set_value; print "[2] value for $target_field: ($set_value)\n";
    And supposed that the form URL is http://localhost/form.html, the program produces the following when run:
    $ perl tick.pl http://localhost/form.html [1] value for passenger: (undef) [2] value for passenger: (somevalue)
    HTH,

    Open source softwares? Share and enjoy. Make profit from them if you can. Yet, share and enjoy!

Re: WWW::Mechanize on broken HTML
by kyle (Abbot) on Jun 21, 2007 at 03:19 UTC

    I'm not an HTML guru, but I don't see anything wrong with the HTML you posted. Maybe you should show us the Perl you've written along with an explanation of what you expect it to do and how it is not meeting those expectations (i.e., what does it do).

Re: WWW::Mechanize on broken HTML
by gecko (Initiate) on Jun 21, 2007 at 08:43 UTC
    *wacks self in the head* Yeah, that was a brisk conclusion to jump to.. turns out i had just selected the wrong form by accident.. and there i was rewriting response objects. Thanks as usual for all the help, working perfectly now!
Re: WWW::Mechanize on broken HTML
by halley (Prior) on Jun 22, 2007 at 17:26 UTC
    Looks like you answered that one, but I had a similar problem, where the company used a checkbox field, but gave no value="" attribute at all. The browser just ignores it, but I had to rewrite the perceived html to fix the errors before I could ->tick() it with the form routines.

    --
    [ e d @ h a l l e y . c c ]