3

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>
Andy Lester
  • 91,102
  • 13
  • 100
  • 152
cppcoder
  • 22,227
  • 6
  • 56
  • 81
  • 1
    If you are dealing with a document that depends on JavaScript, don't use WWW::Mechanize, it doesn't do JavaScript. Use WWW::Mechanize::Firefox or WWW::Selenium instead. – Quentin May 31 '12 at 08:55
  • How to find if the page is JS? I am not familiar to web development. – cppcoder May 31 '12 at 08:56
  • 1
    Two key indicators are: (1) Form controls that aren't in forms and (2) Links with `href="#"` that aren't labeled "Top of page" – Quentin May 31 '12 at 09:00
  • @Quentin - I see both these indicators in my page. Thanks for this tip. Can I use `WWW::Selenium` for web page automation? I want to complete a web flow, not test it. – cppcoder May 31 '12 at 09:02
  • Yes. You just ignore the last step of the process (where you make assertions). – Quentin May 31 '12 at 09:03

4 Answers4

1

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?

Lee Goddard
  • 10,680
  • 4
  • 46
  • 63
1
$mech->tick( 'name' => undef );
Daniel Böhmer
  • 14,463
  • 5
  • 36
  • 46
tread
  • 10,133
  • 17
  • 95
  • 170
0

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.

christurnerio
  • 1,469
  • 11
  • 13
0

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;
daxim
  • 39,270
  • 4
  • 65
  • 132
  • Then by-pass the form-based methods. Use Wireshark, Firebug or similar to find out the details of the form key/value pairs of actual resulting HTTP request when the form is submitted, then copy/mimick it with [`submit_form(with_fields => \%form_kv)`](http://p3rl.org/WWW::Mechanize#mech-submit_form-...-). – daxim May 30 '12 at 18:43
  • This is the HTML for the checkbox ``. But `Mechanize::find_all_inputs()` does not list this. Any idea why? I am also trying to find the HTTP request as you suggested. – cppcoder May 31 '12 at 08:39
  • When I check the checkbox, a link gets enabled. But on clicking the link, I dont see any HTTP request. But it goes to a different page. Is that possible? – cppcoder May 31 '12 at 08:45