How to check a checkbox using WWW::Mechanize?

3.3k Views Asked by At

I read the Perl WWW::Mechanize module and this is the syntax:

$mech->tick( $name, $value [, $set] )

But, when I checked the page source of the web page, this is what I found:

<div class="key-name" title="GLOBAL_PROCESSING">GLOBAL_PROCESSING</div>
    <div class="col-50 col-left">
    <div class="string-controls">
    <a href="#" class="control-expand-toggle"></a>
    <a href="#" class="control-activity-toggle ">0</a>
    <input type="checkbox" class="control-select-string">
    </div>

I do not see an id and value for the checkbox field. How should I do this?

Also the check box is not part of any form. How can I refer to this checkbox in Mechanize?

HTML code

<div id="edit-controls-leftside" class="clear-fix">
<div class="col-left">
<label>
<input id="select-all-visible" class="" type="checkbox">
&nbsp;Select all visible
</label>
<a id="expand-all" class="blue-on-dark-blue text-link arrow-leftside-down"     href="#">Show key names</a>
<a id="show-modify-nav" class="blue-on-dark-blue text-link arrow-leftside-right disabled" href="#">Modify selected...</a>
<nav id="modify-nav" style="display: none;">
<a id="show-order-translation" class="sub-nav-item" href="#">Order translations</a>
4

There are 4 best solutions below

0
christurnerio On

Here are the some workarounds for checking checkboxes without values using perl Mechanize.

From the Mechanize manual.

How do I check a checkbox that doesn't have a value defined?

Set it to to the value of "on".

$mech->field( my_checkbox => 'on' );

Another option found here.

$form->find_input('checkbox_id')->check();

The only problem is that your checkbox doesn't have a name/id either. Hope someone else can chime in with how to grab the checkbox using something other than the name/id. I'll keep looking.

EDIT:

You may be able to use:

$mech->find_all_inputs( ... criteria ... )

To locate the checkbox by type and/or classname since it doesn't have a name or id.

3
daxim On

Use the method check in HTML::Form. Documentation quote:

Some input types represent toggles that can be turned on/off. This includes "checkbox" and "option" inputs. Calling this method turns this input on without having to know the value name.

Code that works with your HTML:

my $w = WWW::Mechanize->new;
$w->get('file:///tmp/10775965.html');
[$w->forms->[0]->inputs]->[0]->check;
0
Lee Goddard On

Why are you trying to do this?

Because the checkbox has no name or value, it will not be part of any submission, even if it was part of a form, which it is not.

Do you think the checkbox is used by JavaScript, to toggle other checkboxes?

0
tread On
$mech->tick( 'name' => undef );