perl - 无法对 WWW/Mechanize.pm 第 2566 行的未定义值调用方法 "header"

标签 perl www-mechanize

我只是在 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/

相关文章:

perl - 来自编码函数的检查参数

c - 支持边标签的图库

php/perl 或其他能够自动验证启用智能卡的网站的网络脚本

perl - 如何使用 Perl 的 LWP 登录 Web 应用程序?

Perl WWW::Mechanize -- 获取 URL 的身份验证错误

perl - WWW::Mechanize->get 错误(不支持 HTTPS)

Perl pgrep 返回多个 pid

perl - 用Perl填充div表单

perl - 如何使用和调试 WWW::Mechanize?

perl - DBIx::Class 使用函数更新 Inflate 列