我正在编写一个简单的代码来登录一个网站进行学习。
我收到一条错误消息,提示"未定义表单">
我如何知道表单名称?
下面是代码片段(我从这个论坛找到它(。
use strict;
use warnings;
use WWW::Mechanize;
use HTTP::Cookies;
my $mech = WWW::Mechanize->new();
my $url = "http://www.something.net";
$mech->cookie_jar->set_cookie(0,"start",1,"/",".something.net");
$mech->get($url);
$mech->form_name("frmLogin");
$mech->set_fields(user=>'user',passwrd=>'password');
$mech->click();
$mech->save_content("logged_in.html");
代码看起来正常吗?
表单的名称(如果有(嵌入在要检索的内容中。例如,如果您查看此页面的源代码,您会发现许多form
元素。这个的 id add-comment-44827103
:
<form id="add-comment-44827103"
class=""
data-placeholdertext="Use comments to ask for more information or suggest improvements. Avoid answering questions in comments."></form>
您可以使用 $mech->forms
检索它们。此调用返回可以进一步查询的HTML::Form
对象的列表。
my ($form) = $mech->forms; # note ($var)=... for list context
my $form_id = $form->attr("id") || die "form on page doesn't have 'id' attr";
$mech->form_id($form_id);
...
还有$mech->form_number( index )
电话
$mech->form_number(2); # select the 2nd form on the page