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

Hi all,

  I have been working on a program that traverses the website of a third party company, so I have no influence on or information about the page. I've been able to successfully tick checkboxes before, but in this case there is a twist.

  The page has an input value of:

<input type="checkbox" name="selrow0" value=" " onclick="clicksel(this +)">
  And the 'clicksel' JS function is:
function clicksel( itm ) { if (itm.checked) { if ((document._LocationSelection.sel.value != itm.name ) && ( +document._LocationSelection.sel.value != "")) { box = eval("document._LocationSelection." + document._Loca +tionSelection.sel.value); box.checked = false; } else { document._LocationSelection.sel.value = itm.name; } } else if (document._LocationSelection.sel.value == itm.name) { document._LocationSelection.sel.value = ""; } }
  As I understand it, the JS sets the value of the checkbox to the name of the checkbox that was ticked and unchecks any others. However, looking through WWW::Mechanize (and HTML::Form) it looks like it expects the input value (space, in this case). Why didn't they use radio? Anyway...
my $input = $agent->current_form->find_input("selrow0", 'checkbox', 0) +; $input->value("selrow0");
  And several variants of cause an error like this:
Illegal value 'selrow0' for field 'selrow0' at ./check_avail.lib line +285
  The '$input->value("selrow0");' being line 285 in this example.

  I've tried several different things, including directly accessing and changing the checkboxes object in WWW::Mechanize ('current', 'name' and 'value' hash values, specifically), but in all cases when I submit the form I get an error saying that I didn't check any boxes (error from the remote website).

  I've bashed my head against the wall on this so much that it's now flat enough to use as a table top... Help?

Digi

PS - Perl 5.8.8 on Ubuntu (devel) and Debian (server), in case it matters.

Replies are listed 'Best First'.
Re: Ticking checkboxes in WWW::Mechanize, with a twist
by NetWallah (Canon) on Sep 13, 2008 at 17:34 UTC
    Here is how I interpret the site's behaviour:

    Instead of looking for the "checked" property of the checkbox, the site wants the "_LocationSelection" value defined.

    The checkbox(es) are used to set/clear this.

    If you find a way to modify the value of "_LocationSelection.sel" without messing with checkboxes, you can probably proceed (until you hit the next issue).

    I looked for a way to use Mechanize to handle "onclick" for a checkbox, but could not find such a beast.

    Good luck.

         Have you been high today? I see the nuns are gay! My brother yelled to me...I love you inside Ed - Benny Lava, by Buffalax

      You were exactly right, I don't know how I missed that.

      Wow, I feel silly. I was so focused on the checkbox input field I totally missed it. *sigh*

      Thank you kindly!

      Digi