我只是在 Facebook 上使用 WWW::Mechanize
模块进行测试,当我尝试运行下面的代码时,它返回一个错误
Can't call method "header" on an undefined value at C:/Strawberry/perl/vendor/lib/WWW/Mechanize.pm line 2566.
#!/usr/bin/perl -w
use WWW::Mechanize;
my $mech = WWW::Mechanize->new();
# Connect to server
$mech->get( "https://www.facebook.com" );
$mech->success or die $mech->response->status_line;
# Log into server
$mech->field('email', '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="6e1616162e161616400d0103" rel="noreferrer noopener nofollow">[email protected]</a>');
$mech->field('pass', 'xxxxxxx');
$mech->click_button(value => 'Log In');
最佳答案
您的页面正在以英语以外的其他语言打开。这就是您收到该错误的原因。如果你强行打开英文页面,那么错误就会消失。尝试以下地址:
$mech->get( "https://en-gb.facebook.com/" );
或者,您可以直接单击使用以下方法获取的 HTML::Form::SubmitInput
实例:
$mech->current_form()->find_input( undef, 'submit');
或者,由于表单中只有一个单击按钮,您可以使用不带参数的click
。
$mech->click()
或者按照@Borodin的建议,您可以直接使用(因为电子邮件和密码字段未翻译):
$mech->submit_form( with_fields => {
email => '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5e2626261e262626703d3133" rel="noreferrer noopener nofollow">[email protected]</a>',
pass => 'xxxxxxx'
}
);
关于perl - 无法对 WWW/Mechanize.pm 第 2566 行的未定义值调用方法 "header",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37018236/