gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 02:49:44 24 4
gpt4 key购买 nike

我使用 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 测试模块!他们让生活变得更轻松。

关于perl WWW::Mechanize,链接重定向问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5836563/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com