Perl WWW :: Mechanism, link redirection problem

I am using WWW :: Mechanize :: Shell for testing.

my code is:

#!/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 as follows:

LINK B (web_page_b.html), 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 the HTML form (web_page_x.html)

The question arises:

How can I get web_page_x.html?

thanks

+4
source share
2 answers

Why don’t you first check to see if code containing a redirect (I assume this is a <META> ?) Exists on web_page_b.html , and then go directly to the next page as soon as you are sure that the browser does.

It looks something like this:

  $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 

By the way, if you conduct any checks using WWW::Mechanize , you really need to check Test :: WWW :: Mechanize and other Perl testing modules! They make life so much easier.

+2
source

In case it really is not redirected, it is better to use a regular expression using this follow_link method, and not just plain text.

eg:

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

for another link.

0
source

All Articles