Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

Clicking a checkbox using www::mechanize

by Anonymous Monk
on May 11, 2004 at 19:15 UTC ( #352554=perlquestion: print w/replies, xml ) Need Help??

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

This is my first post to perlmonks. I have been using Perl for about 4-5 years and I need help with a script that I'm writing. I want to click a checkbox using www::mechanize. The html for the checkbox looks like this: <td><input id="chklstCC_0" type="checkbox" name="chklstCC:0" onclick="__doPostBack('chklstCC:0','')" language="javascript" /><label for="chklstCC_0">Visa</label></td> When the checkbox VISA is clicked, the javascript __doPostBack brings up more fields (newfield1,newfield2) on the htmlpage that I want to fill in as well. My script never finds newfield1 nor newfield2 because the checkbox is never getting clicked by www::mechanize. My code for checking the checkbox looks like this:
my $agent = WWW::Mechanize->new(); my $chk_visa = "chklstCC:0"; $agent->tick($chk_visa);
Is this correct?

Replies are listed 'Best First'.
Re: Clicking a checkbox using www::mechanize
by nmcfarl (Pilgrim) on May 11, 2004 at 19:47 UTC

    It is correct, but it won't do what you want it to do. WWW::Mechanize emulates a browser without javascript.

    If you want the script to work you will need to either fake the javascript or embed JavaScript::SpiderMonkey which is almost always too much work.

    Have fun! (and see this thread WWW::Mechanize and javascript)

    Updated: The usage is not correct as jeffa points out. Must remember to check facts that I 'know'.

      What do you mean by "fake" the javascript?

        To fake the javascript, you read it , and figure out what it does. Then do those actions in perl (or ignore them if you don't care.) In this case it might look like code that follows , but it all depends on what the javascript does.

        $agent->submit_form( form_name => 'hidden_form', fields => { 'newfield1' => 'val1', 'newfield2' => 'val2' }, );
Re: Clicking a checkbox using www::mechanize
by jeffa (Bishop) on May 11, 2004 at 19:57 UTC

    geekgrrl and nmcfarl are correct about Javascript, but your tick() usage is wrong. You need to pass the value as the second arg:

    Here are two test scripts for you, the first is a CGI script and the second is a W::M bot that queries the CGI script and tests its "return" value.

    UPDATE: (a reply)
    No value? Then those aren't "real" checkboxes, are they? Value is a required attribute for the checkbox input type. I gave you correct usage for correct HTML. The checkboxes you are trying to check rely on Javascript, and you will too if you want this bot to work. I have used Javascript with success before, code is at (jeffa) Re: Encrypt web files!. Good luck, you're gonna need it. :/


    (the triplet paradiddle with high-hat)
      Could I pay you to help me? If so, how much do you expect to get?
      But the checkbox has no "value" associated within the input field. I have tried using $bot->tick('chklstCC:0','Visa'); and it did not work either.
Re: Clicking a checkbox using www::mechanize
by geekgrrl (Pilgrim) on May 11, 2004 at 19:45 UTC
Re: Clicking a checkbox using www::mechanize
by Roy Johnson (Monsignor) on May 11, 2004 at 21:27 UTC
    If newfield1 and newfield2 are hidden, you can still fill them in with Mechanize. It will give you a warning, but will work.

    The PerlMonk tr/// Advocate
      And you can ignore that warning, if you want. Check the WWW::Mechanize FAQ for details.


Re: Clicking a checkbox using www::mechanize
by Anonymous Monk on May 11, 2004 at 19:53 UTC
    What do you mean by fake the javascript? Do you have an example?

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://352554]
Approved by McMahon
Front-paged by McMahon
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (2)
As of 2023-03-29 23:37 GMT
Find Nodes?
    Voting Booth?
    Which type of climate do you prefer to live in?

    Results (73 votes). Check out past polls.