如何使用Perl的WWW :: Mechanize访问没有名称或ID的表单?

时间:2010-02-14 08:56:38

标签: perl www-mechanize

我的Perl程序出现问题。此程序登录到特定网页并填写消息的文本区域和移动号码的输入框。单击“发送”按钮后,消息将发送到指定的号码。我已经把它用于发送消息了。但问题是我无法使其接收消息/回复。我在Perl中使用WWW::Mechanize模块。这是我的代码的一部分(用于接收msgs):

$username = 'suezy';
$password = '123';
$url = 'http://..sample.cgi';

# ...

$mech->credentials($username, $password);  
$mech->get($url);

$mech->submit(); 

我的问题是,表单没有显示名称。此表单中有两个按钮,但我无法选择要单击的按钮,因为没有指定名称且ids包含空格(例如,表单名称='接收消息'..) 。我需要点击第二个按钮,“接收”。

问题是,如何在不使用名称的情况下使用mechanize模块访问表单和按钮?

4 个答案:

答案 0 :(得分:4)

您可以将form_number参数传递给submit_form方法。

或者调用form_number方法来影响稍后调用click或field所使用的表单。

答案 1 :(得分:4)

您是否尝试过使用HTTP Recorder? 查看文档并尝试查看它是否为您提供了合理的结果。

答案 2 :(得分:3)

看到表单上只有两个按钮,ysth的建议应该很容易实现。

use strict;
use warnings;
use WWW::Mechanize;

my $username = "suezy";
my $password = "123";
my $url = 'http://.../sample.cgi';
my $mech = WWW::Mechanize->new();

$mech->get($url);
$mech->credentials($username,$password);

然后:

$mech->click_button({number => 1});       # if the 'Receive' button is 1

或者:

$mech->click_button({number => 2});       # if the 'Receive' button is 2

一个反复试验的案例足以让你找出你点击的按钮。

修改

我假设已经选择了相关表格。如果不是:

$mech->form_number($formNumber);

其中$formNumber是相关网页上的表单编号。

答案 3 :(得分:1)

$mech->form_with_fields('username');

将选择包含名为username的字段的表单。 HTH