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

发布于 2024-11-04 04:26:09 字数 927 浏览 5 评论 0原文

我使用 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);   

........................ ........................ ........................ 等等,

问题是下一个:

重定向到 web_page_x.html

LINK B (web_page_b.html),如果我打印 $mech->content() 的内容,则

,显示 web_page_b.html但我需要显示 web_page_x .html,自动提交 HTML 表单 (web_page_x.html)

问题是:

如何获取 web_page_x.html ?

谢谢

I use WWW::Mechanize::Shell to test stuff.

my code is this:

#!/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);   

........................
........................
........................
etc, etc.

the problem is the next:

LINK B (web_page_b.html), make a redirect to web_page_x.html

if I print the contents of $mech->content(), display web_page_b.html

but i need to display web_page_x.html,to automatically submit a HTML form (web_page_x.html)

The question is:

How I can get web_page_x.html ?

thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我的鱼塘能养鲲 2024-11-11 04:26:09

为什么不首先测试一下 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 测试模块!它们让生活变得更加轻松。

Why don't you first test to see if the code containing the redirect (I'm guessing it's a <META> tag?) exists on web_page_b.html, then go directly to the next page once you're sure that that's what a browser would have done.

This would look something like:

  $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

Incidentally, if you're doing any kind of testing with WWW::Mechanize, you should really check out Test::WWW::Mechanize and the other Perl testing modules! They make life a lot easier.

难得心□动 2024-11-11 04:26:09

如果它没有真正重定向,那么您最好将正则表达式与该 follow_link 方法一起使用,而不仅仅是纯文本。

例如:

$mech->follow_link(url_regex => qr/web_page_b/i , n => 1);  

其他链接也一样。

In case it doesn't really redirect, then you better use regex with that follow_link method rather than just plain text.

such as:

$mech->follow_link(url_regex => qr/web_page_b/i , n => 1);  

same for the other link.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文