WWW::Mechanize::Firefox::Cookbook - Recipes for using WWW::Mechanize::Firefox
Note that WWW::Mechanize::Firefox is quite similar in its API to WWW::Mechanize, so many of the recipes in WWW::Mechanize::Cookbook apply to it as well.
use WWW::Mechanize::Firefox; my $mech = WWW::Mechanize::Firefox->new(); $mech->get('http://google.com');
use WWW::Mechanize::Firefox; my $mech = WWW::Mechanize::Firefox->new( launch => '/path/to/firefox', ); $mech->get('http://google.com');
use WWW::Mechanize::Firefox; my $mech = WWW::Mechanize::Firefox->new( tab => qr/^Google/, ); $mech->get('http://google.com');
use WWW::Mechanize::Firefox; my $mech = WWW::Mechanize::Firefox->new( tab => 'current', ); $mech->get('http://google.com');
use WWW::Mechanize::Firefox; my $mech = WWW::Mechanize::Firefox->new( activate => 1, ); $mech->get('http://google.com');
my $mech = WWW::Mechanize::Firefox->new( ); ... $mech->activateTab();
use WWW::Mechanize::Firefox; my $mech = WWW::Mechanize::Firefox->new( tab => 'current', ); $mech->get('http://google.com', ':content_file' => 'google_com.html' );
use WWW::Mechanize::Firefox; my $mech = WWW::Mechanize::Firefox->new( tab => 'current', ); $mech->get('http://google.com'); $mech->save_content('google_com.html', 'google_com files');
Some sites generate content after the DOMContentLoaded event has fired. To wait until an element becomes available, use the following code:
DOMContentLoaded
# The submit button is generated after the page has loaded my $retries = 10; while ($retries-- and ! $mech->is_visible( xpath => '//*[@id="submit"]' )) { sleep 1; }; die "Timeout" unless $retries; # Now the element exists $mech->click({xpath => '//*[@id="submit"]'});
Some sites display an hourglass or some other indicator to tell you to wait. To wait until such an element becomes invisible, use the following code:
my $retries = 10; while ($retries-- and $mech->is_visible( xpath => '//*[@id="hourglass"]' )) { sleep 1; }; die "Timeout while waiting for application" unless $retries; # Now the hourglass is not visible anymore
In the case that you want to navigate the user to a specific place and then take over manually, you can prevent the automatic closing of the browser tab. Either create your $mech object like this:
$mech
my $mech = WWW::Mechanize::Firefox->new( autoclose => 0, );
or decide at runtime whether to close the tab:
$mech->autoclose_tab(0);
This does currently not work.
my $browser = $mech->repl->expr('window.getBrowser()'); my $eventlistener = progress_listener( $browser, onLocationChange => \&onLocationChange, ); while (1) { $mech->repl->poll(); sleep 1; };
This will take a "screenshot" which also includes plugins like Flash.
my $png = $mech->content_as_png(); open my $fh, '>', 'page.png' or die "Couldn't save to 'page.png': $!"; binmode $fh; print {$fh} $png; close $fh;
Also see the file screenshot.pl included and installed through the distribution.
screenshot.pl
my $rect = { left => 0, top => 0, width => 200, height => 200, }; my $png = $mech->content_as_png(undef, $rect); open my $fh, '>', 'page.png' or die "Couldn't save to 'page.png': $!"; binmode $fh; print {$fh} $png; close $fh;
$mech->get('mypage'); my @errors = map {$_->{message}} $mech->js_errors(); if (@errors) { die "Found errors on page: @errors"; };
alert()
$mech->eval_in_page('alert("Hello");', { alert => sub { print "Captured alert: '@_'\n" } } );
Just pass empty lists for the lists of events to invoke
$mech->field( 'myfield', 'myvalue', [], [] );
ftp
Not implemented - this requires instantiating and passing a nsIURI object instead of a nsILocalFile .
nsIURI
nsILocalFile
You can use ->save_url to transfer files. $localname can be a local filename, a file:// URL or any other URL that allows uploads, like ftp://.
->save_url
$localname
file://
ftp://
$mech->save_url('file://path/to/my/file.txt' => 'ftp://myserver.example/my/file.txt');
Max Maischein corion@cpan.org
corion@cpan.org
Contributed examples contain the original author's name.
Copyright 2010, Max Maischein.
All Rights Reserved. This module is free software. It may be used, redistributed and/or modified under the same terms as Perl itself.
To install WWW::Mechanize::Firefox, copy and paste the appropriate command in to your terminal.
cpanm
cpanm WWW::Mechanize::Firefox
CPAN shell
perl -MCPAN -e shell install WWW::Mechanize::Firefox
For more information on module installation, please visit the detailed CPAN module installation guide.