Perl的机械化follow_link失败
问题描述:
我试图使用登录到网站下面的代码Perl的机械化follow_link失败
my $mech = WWW::Mechanize->new(autosave=>1);
$mech->cookie_jar(HTTP::Cookies->new());
$mech->get($url);
$mech->follow_link(text => 'Sign In');
$mech->click();
$mech->field(UserName => "$username");
$mech->field(Password => "$password");
$mech->submit();
但在follow_link在href包含两个前斜线如(//test/sso-login
),因此follow_link正在考虑它作为整个URL和它的失败如下
Error GETing http://test/sso-login: Can't connect to test:80 (Bad hostname)
我不能改变href,因为它是我的控制权。有没有办法来克服这个问题,并使其完整的URL附加这个href。
答
当然。您可以修改机甲在看着你打电话follow_link()
之前的HTML:
my $html = $mech->content;
$html =~ s[//test/sso-login][http://example.com/test/sso-login]isg;
$mech->update_html($html);
见documentation了解详情。在该页面上搜索“update_html”。