WWW::Mechanize 1.18 review
DownloadWWW::Mechanize is a handy web browsing in a Perl object. SYNOPSIS WWW::Mechanize, or Mech for short, helps you automate interac
|
|
WWW::Mechanize is a handy web browsing in a Perl object.
SYNOPSIS
WWW::Mechanize, or Mech for short, helps you automate interaction with a website. It supports performing a sequence of page fetches including following links and submitting forms. Each fetched page is parsed and its links and forms are extracted. A link or a form can be selected, form fields can be filled and the next page can be fetched. Mech also stores a history of the URLs you've visited, which can be queried and revisited.
use WWW::Mechanize;
my $mech = WWW::Mechanize->new();
$mech->get( $url );
$mech->follow_link( n => 3 );
$mech->follow_link( text_regex => qr/download this/i );
$mech->follow_link( url => 'http://host.com/index.html' );
$mech->submit_form(
form_number => 3,
fields => {
username => 'mungo',
password => 'lost-and-alone',
}
);
$mech->submit_form(
form_name => 'search',
fields => { query => 'pot of gold', },
button => 'Search Now'
);
Mech is well suited for use in testing web applications. If you use one of the Test::*, like Test::HTML::Lint modules, you can check the fetched content and use that as input to a test call.
use Test::More;
like( $mech->content(), qr/$expected/, "Got expected content" );
Each page fetch stores its URL in a history stack which you can traverse.
$mech->back();
If you want finer control over over your page fetching, you can use these methods. follow_link and submit_form are just high level wrappers around them.
$mech->follow( $link );
$mech->find_link( n => $number );
$mech->form_number( $number );
$mech->form_name( $name );
$mech->field( $name, $value );
$mech->set_fields( %field_values );
$mech->set_visible( @criteria );
$mech->click( $button );
WWW::Mechanize is a proper subclass of LWP::UserAgent and you can also use any of LWP::UserAgent's methods.
$mech->add_header($name => $value);
Please note that Mech does NOT support JavaScript. Please check the FAQ in WWW::Mechanize::FAQ for more.
Requirements:
Perl
WWW::Mechanize 1.18 keywords