perl WWW ::机械化,链接重定向问题
我使用WWW :: Mechanize :: Shell来测试东西。perl WWW ::机械化,链接重定向问题
我的代码是这样的:
#!/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- $>含量()的内容,显示web_page_b.html
,但我需要显示web_page_x.html,自动提交一个HTML表格(web_page_x.html)
的问题是:
我如何获得web_page_x.html?
感谢
在情况下,它并没有真正重定向,那么你最好使用正则表达式与follow_link
方法,而不仅仅是纯文本。
如:
$mech->follow_link(url_regex => qr/web_page_b/i , n => 1);
同为其他链路。
非常感谢你,但不起作用 – Sanx 2011-05-03 17:21:23
为什么不首先测试以确定包含重定向的代码(我猜这是一个<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模块的测试!他们让生活变得更容易。
什么样的重定向? Mech会自动跟踪HTTP重定向,除非你不告诉它。如果这是一个Meta标签或Javascript重定向,则需要对其进行反向工程并自行构建等效的HTTP请求。或者使用像Selenium这样基于浏览器的系统。 – friedo 2011-04-29 19:48:47