简体   繁体   English

Perl使用WWW :: Mechanize设置一个没有值的单选按钮

[英]Perl Using WWW::Mechanize to set a radio button that has no value

I have to say I am new to Perl, maybe made about 3 or 4 scripts so far, and I find Perl decently easy to understand and use. 我不得不说我是Perl的新手,到目前为止可能有大约3或4个脚本,我发现Perl很容易理解和使用。

Anyways, I have a problem with my current script. 无论如何,我的当前脚本有问题。 I have been stuck on this problem for a while and just can't seem to find a fix for it. 我一直坚持这个问题一段时间,似乎无法找到它的修复。

After traversing a certain website I come to a page that has me click between two radio buttons and hit a download button. 在遍历某个网站后,我来到一个页面,让我点击两个单选按钮并点击下载按钮。

I need to get my script to select either button and hit download, but the source of the page does not give me a value to set the radio button to when using WWW::Mechanize . 我需要让我的脚本选择按钮和点击下载,但是当使用WWW::Mechanize时,页面源不会给我一个值来设置单选按钮。

Here is a little bit of the web page source. 这是一些网页源代码。

<input type='radio' onclick="url('URL GOES HERE')">

That is pretty much it for the two buttons. 对于这两个按钮来说几乎就是这样。 I noticed that when I did choose one and looked at the source, the code changes to 我注意到当我选择一个并查看源代码时,代码会变为

<input type='radio' checked onclick="url('URL GOES HERE')">

Yet I can't have WWW::Mechanize set one automatically because I have no idea what to put in the value. 但我不能让WWW::Mechanize自动设置一个,因为我不知道该放入什么值。 I have tried on , checked , onclick , true , checked onclick , select , selected , but to no avail. 我曾尝试oncheckedonclicktruechecked onclickselectselected ,但无济于事。

Here is the line of code that pertains to this 以下是与此相关的代码行

$mech->set_visible([radio => 'checked onclick']);

Any help would be appreciated, first time here as well. 任何帮助将不胜感激,第一次也在这里。

I forgot to mention that I am working with a computer at work that has a lot of restrictions for now. 我忘了提到我正在使用一台计算机,这项计算机目前有很多限制。 So I can't get Firefox or Selenium Server on the computer. 所以我无法在计算机上安装Firefox或Selenium Server。

Edit 编辑

I believe the problem that I might be having also is that this stuff might be in JavaScript. 我相信我可能遇到的问题是这些东西可能是在JavaScript中。 I don't have much knowledge in HTML but the source code looked like it was until I saw JavaScript somewhere in the heading? 我对HTML没有太多的了解,但源代码看起来就像是在标题中的某个地方看到了JavaScript? Anyways, I remembered that WWW::Mechanize doesn't really support JavaScript and so maybe this is the problem. 无论如何,我记得WWW::Mechanize并不真正支持JavaScript,所以也许这就是问题所在。

Thanks for the replies guys, I really appreciated it. 感谢回复人员,我真的很感激。 But after a ton of digging around and careful inspection, my boss and I sort of realized that all the download links are very similar, and all I really need to do is make the script custom create the links to the downloads instead of going through the hassle of taversing multiple web pages. 但经过大量的挖掘和仔细检查后,我的老板和我有点意识到所有下载链接都非常相似,我真正需要做的就是让脚本自定义创建链接到下载而不是通过捣乱多个网页的麻烦。 Wish I saw this sooner. 希望我早点看到这个。

HTML::Form does not offer an API to activate unnamed radio buttons. HTML :: Form不提供激活未命名单选按钮的API。 You can read the distinguishing attribute by digging into the innards. 您可以通过挖掘内部来阅读区别属性。 This should work (mostly untested): 这应该工作(大多数未经测试):

use WWW::Mechanize qw();
my $w = WWW::Mechanize->new;
$w->get('file:///tmp/so11767998.html');
for my $input (($w->forms)[0]->inputs) {
    $input->check if q{url('URL GOES HERE')} eq $input->{onclick}
}

You need to find what's happens after you select your radio button in the browser GUI. 您需要在浏览器GUI中选择单选按钮后查找发生的情况。 There are two ways for this: 有两种方法:

  1. you can check url() Javascript subroutine 你可以检查url()Javascript子程序

  2. or you can record your browser request after you submit target form. 或者您可以在提交目标表单后记录您的浏览器请求。

I recommend to use Firefox's HTTPFox extension for this. 我建议使用Firefox的HTTPFox扩展。 So you just select you input, start the session in HTTPFox and submit the form. 所以你只需选择输入,在HTTPFox中启动会话并提交表单。 Next you check what data was POSTed (or GETed) and just replicate same fields in your Mechanize script. 接下来,检查POSTed(或GETed)的数据,并在Mechanize脚本中复制相同的字段。 Something like: 就像是:

$mech->submit_form(
    form_name => "form_name",
    fields => {
        input1_name => "value1",
        input2_name => "value2",
.....
    },
);

I had this issue too , and resolved it by calling a Javascript to click Radio Button. 我也有这个问题,并通过调用Javascript单击单选按钮解决它。

$mech->eval_in_page('document.getElementById("id_radiobutton").checked = true;');

This worked for me. 这对我有用。

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

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