如何使用WWW::Mechanize和下拉菜单在Perl中导航网页?

如何使用WWW::Mechanize和下拉菜单在Perl中导航网页?,perl,Perl,我遇到了一个问题,我想做的是: 我打开一个带有WWW::Mechanize的网页,填写用户名和密码并登录 我遇到的问题是,登录后,我必须从下拉列表中选择值 然后我必须按提交 我该怎么做 我使用的代码是: #!/usr/bin/perl use LWP::UserAgent; use WWW::Mechanize; use HTTP::Cookies; use strict; my $username="123456"; my $password="XXXXX"; my $project="S

我遇到了一个问题,我想做的是:

我打开一个带有WWW::Mechanize的网页,填写用户名和密码并登录

我遇到的问题是,登录后,我必须从下拉列表中选择值 然后我必须按提交

我该怎么做

我使用的代码是:

#!/usr/bin/perl
use LWP::UserAgent;
use  WWW::Mechanize;
use HTTP::Cookies;
use strict;

my $username="123456";
my $password="XXXXX";
my $project="Systems";
my $agent = WWW::Mechanize->new();
$agent->get('http://www.XXXXX.com');
$agent->form_name("login_form");
$agent->field("txtLoginId", $username);
$agent->field("txtPassword", $password);
$agent->submit();
#Till now it has success full logined, From here it has to select one value from a drop #down box
$agent->form_name("frmProject");
$agent->field("cboProject", $project);
my $response=$agent->submit();

if ($response->is_success){
  print "\nContent:\n";
  print $response->as_string;
}elsif ($response->is_error){
  print $response->error_as_HTML;
}

WWW::Mechanize有一个
单击
方法,您可以使用该方法单击按钮。或者,研究
submit\u form
方法,您可以为该方法指定所有表单元素的值。如果页面使用javascript,则WWW:Mechanize可能不适合您的任务(请参见示例以获取替代方案)。

您需要使用。如果您知道该值(而不是显示文本),请使用以下命令:

$agent->form_name("frmProject");
$agent->select('cboProject', $project);
my $response = $agent->submit();
如果你不去看一眼。它说你必须这样做:

# Find the correct input element
my ($projectlist) = $agent->find_all_inputs( name => 'cboProject' );
# Look up the value of the option

my %name_lookup;
@name_lookup{ $projectlist->value_names } = $projectlist->possible_values;

# use the display-text to get the correct value
my $value = $name_lookup{ $project };

完成后,可以使用提交页面

$agent->click_button('name_of_the_submit_button');
但是,如果您必须单击的按钮是默认操作,
$agent->submit()
也应该这样做。

您应该始终
使用警告
以及
使用严格的
,以从perl获得最大限度的帮助