简体   繁体   English

如何使用 WWW::Mechanize 选中复选框?

[英]How to check a checkbox using WWW::Mechanize?

I read the Perl WWW::Mechanize module and this is the syntax:我阅读了 Perl WWW::Mechanize模块,这是语法:

$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.我没有看到复选框字段的 ID 和值。 How should I do this?我该怎么做?

Also the check box is not part of any form.此外,复选框不是任何形式的一部分。 How can I refer to this checkbox in Mechanize?如何在 Mechanize 中引用此复选框?

HTML code HTML代码

<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>

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?您认为 JavaScript 使用复选框来切换其他复选框吗?

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

Here are the some workarounds for checking checkboxes without values using perl Mechanize .以下是使用 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".将其设置为“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.唯一的问题是您的复选框也没有名称/ID。 Hope someone else can chime in with how to grab the checkbox using something other than the name/id.希望其他人可以加入如何使用名称/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.按类型和/或类名定位复选框,因为它没有名称或 ID。

Use the method check in HTML::Form .使用HTML::Form方法check 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:适用于您的 HTML 的代码:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM