perl WWW :: Mechanize,链接重定向问题

| 我使用WWW :: Mechanize :: Shell进行测试。 我的代码是这样的:
#!/usr/bin/perl
use WWW::Mechanize;
use HTTP::Cookies;

my $url = \"http://mysite/app/login.jsp\";
my $username = \"username\";
my $password = \"asdfasdf\";
my $mech = WWW::Mechanize->new();
$mech->cookie_jar(HTTP::Cookies->new());
$mech->get($url);
$mech->form_number(1);
$mech->field(j_username => $username);
$mech->field(j_password => $password);
$mech->click();
$mech->follow_link(text => \"LINK A\", n => 1);   
$mech->follow_link(text => \"LINK B\", n => 1);   
........................... ........................... ........................... 等等等 问题是下一个: LINK B(web_page_b.html),重定向到web_page_x.html 如果我打印$ mech-> content()的内容,则显示web_page_b.html 但我需要显示web_page_x.html,才能自动提交HTML表单(web_page_x.html) 问题是: 我如何获得web_page_x.html? 谢谢     
已邀请:
为什么不先测试一下包含重定向的代码(我猜它是一个
<META>
标记?)是否存在于
web_page_b.html
上,然后在确定该位置之后直接转到下一页这是浏览器会执行的操作。 这看起来像:
  $mech->follow_link(text => \"LINK B\", n => 1);
  unless($mech->content() =~ /<meta http-equiv=\"refresh\" content=\"5;url=(.*?)\">/i) {
     die(\"Test failed: web_page_b.html does not contain META refresh tag!\");
  }
  my $expected_redirect = $1;      # This should be \'web_page_x.html\'
  $mech->get($expected_redirect);  # You might need to add the server name into this URL
顺便说一句,如果您正在使用
WWW::Mechanize
做任何类型的测试,则应该真正查看Test :: WWW :: Mechanize和其他Perl测试模块!它们使生活变得更加轻松。     
如果它没有真正重定向,那么最好将regex与
follow_link
方法配合使用,而不仅仅是纯文本。 如:
$mech->follow_link(url_regex => qr/web_page_b/i , n => 1);  
其他链接也一样。     

要回复问题请先登录注册