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

I'm creating a script to take screenshot an element of web page. It can take full picture of long page using content_as_png but using element_as_png, only the content visible is captured, the scrollable part is not. Here is the script -
use WWW::Mechanize::Firefox; use File::Spec; my $mech = WWW::Mechanize::Firefox->new( bufsize => 10_000_000, # PNGs might become huge autoclose => false ); $mech->get('http://layout.jquery-dev.net/demos/simple.html'); my $png = $mech->content_as_png(); my @links = $mech->by_id('center'); my $png2 = $mech->element_as_png(@links); open my $fh, '>', 'page.png' or die "Couldn't save to 'page.png': $!"; binmode $fh; print {$fh} $png; close $fh;
Is there an existing way to do it or will I have to dig deeper in code.

Replies are listed 'Best First'.
Re: page screenshot using www::mechanize::firefox
by Corion (Patriarch) on Mar 02, 2012 at 15:54 UTC

    There is a bug in WWW::Mechanize::Firefox 0.59:

    Line 3327 is

    if( isNaN( target_rect.scalex * target_rect.scaley )) {

    ... and should be

    if( isNaN( target_rect.scalex * target_rect.scaley ) || target +_rect.scalex * target_rect.scaley == 0) {

    Version 0.60 will fix this - it should be on CPAN within the next few hours.

    Update: But that bug does not "fix" your desired behaviour. There is no way to take a "complete screenshot" of that page with Javascript enabled, because that page resizes only a part of itself. If that page had an "outer" scrollbar, taking a screenshot would take a screenshot of the whole page, not only of the visible part. As it is, you will have to use the technique already mentioned by Anonymous Monk, take a screenshot of an element, and/or scroll a bit.

      Thanks to the great monks for the quick wisdom.
Re: page screenshot using www::mechanize::firefox
by moritz (Cardinal) on Mar 02, 2012 at 15:05 UTC
      This one does the job perfectly. Now I don't need to start firefox everytime. Thanks.
Re: page screenshot using www::mechanize::firefox
by Anonymous Monk on Mar 02, 2012 at 14:51 UTC
      Good idea, working on it until I strike the right solution.