sadarax has asked for the wisdom of the Perl Monks concerning the following question:

EDIT: Found a solution. Thanks everyone, particularly Corion.

A simple program to take a screencapture of a webpage in a web-browser (in this case Firefox).

#!/usr/bin/perl -w use warnings; use strict; use WWW::Mechanize::Firefox; my $url = "http://cmcc.deviantart.com"; my $outfile = "MyScreenshot.png"; my $telnet_max_buff_len = 10485760; my $mech = WWW::Mechanize::Firefox->new( launch => 'firefox',); my $net_telnet = $mech->repl->repl->client; # Bridge deep into WWW:Tel +net for finer control $net_telnet->telnet->max_buffer_length($telnet_max_buff_len); # Enable + buffer size to handle large webpage $mech->get($url); # Grab the content my $png = $mech->content_as_png(); # Dump to a PNG file open my $out, '>', $outfile or die "Couldn't create '$outfile': $!"; binmode $out; # Enable binary mode print {$out} $png; # Write the file to disk

You will need to have WWW::Mechanize::Firefox installed (which can be done through cpan), and you will need to have the firefox addon MozRepl installed (http://wiki.github.com/bard/mozrepl). Module::Pluggable::Fast is, for some reason, not installable through the CPAN shell. You will need to download it separately and extract and install it manually by doing the following dance:

1. Download Module::Pluggable::Fast
2. Extract the tarball somewhere convenient
3. Open a shell in that directory
4. Type cpan . (including that dot)
5. Watch the module get installed
6. Retry installing WWW::Mechanize::Firefox

This should work. Thanks everyone again.

Replies are listed 'Best First'.
Re: Screencapture with Perl on Linux
by Corion (Patriarch) on Dec 05, 2009 at 14:26 UTC
      AFAI understand the docs, there are two ways to achieve this:

      One has to either know the geometry of the window (I suppose by evaluating window.innerWidth ... ?) to clip it completely

      $mech->content_as_png [TAB, COORDINATES]

      or to get the "html"-tag element (document.getElementsByTagName()) to use

      $mech->element_as_png $element

      correct?

      Or is there a more straight forward way, like skipping COORDINATES defaults to clipping the whole window?

      If not, wouldn't this be a reasonable feature request? 8)

      Cheers Rolf

        In the respository, there now is screenshot.pl, which captures the screen. And both, the tab and the coordinates are optional. What follows is the meat of the used code:

        #!perl -w use strict; use WWW::Mechanize::Firefox; use Getopt::Long; use Pod::Usage; GetOptions( 'mozrepl|m:s' => \my $mozrepl, 'outfile|o:s' => \my $outfile, ) or pod2usage(); $outfile ||= 'screenshot.png'; my @args; if (! @ARGV) { push @args, tab => 'current'; # this needs 0.13 or higher }; my $mech = WWW::Mechanize::Firefox->new( launch => 'firefox', @args ); if (@ARGV) { $mech->get($ARGV[0]); }; my $png = $mech->content_as_png(); open my $out, '>', $outfile or die "Couldn't create '$outfile': $!"; binmode $out; print {$out} $png;

        I'm not sure how I could make it clearer in the documentation that all parameters to ->content_as_png are optional, most likely I'll add a reference to screenshot.pl in the next release. Other suggestions are welcome!

Re: Screencapture with Perl on Linux
by Khen1950fx (Canon) on Dec 05, 2009 at 13:36 UTC
    I used IPC::System::Simple. I took a screenshot of CNN, and everything was there.

    #!/usr/local/bin/perl use strict; use warnings; use IPC::System::Simple qw(capture); my $url = 'http://www.cnn.com'; system("firefox", $url); sleep 30; $url = capture("import -window root MyScreenshot.png"); print $url, "\n";
      Thanks for the effort, but tried it and it did not work. Depending on which number of desktop I execute the script from (run from a terminal), it just takes a picture of the my desktop background or I get a blank screen with the windows borders
Re: Screencapture with Perl on Linux
by LanX (Saint) on Dec 05, 2009 at 13:44 UTC
    My suggestion is to use the proper window ID of your FF.

    Anyway it's not working 100% from shell on my Ubuntu/Gnome box, parts of the images are always broken.

    But since this is not a perl issue, you should better ask in a linux forum or report to ImageMagick.

    Cheers Rolf

    UPDATE: contact

Re: Screencapture with Perl on Linux
by stefbv (Priest) on Dec 05, 2009 at 13:28 UTC
    The code is working fine for me, when runing the script from the command line (on Slackware).
Re: Screencapture with Perl on Linux
by LanX (Saint) on Dec 06, 2009 at 22:42 UTC
    maybe you can do it with xv... I remember doing screenshots with it but I'm not sure if it's scriptable.

    Probably you can determine if the problem comes from ImageMagick or the underlying Ubuntu / X system.

    Cheers Rolf