You can use Win32::GuiTest::FindWindowLike to find the window handle associated with the browser and specify that to screenshot
:
#!/usr/bin/env perl
use strict; use warnings;
use Const::Fast;
use Imager;
use Imager::Screenshot qw( screenshot );
use Win32::GuiTest qw( FindWindowLike SetForegroundWindow );
const my $TYPE => 'bmp';
my @windows = FindWindowLike(0,
'(?:Mozilla Firefox)|(?:Internet Explorer)|(?:Opera)'
);
for my $hwnd (@windows) {
warn "$hwnd\n";
SetForegroundWindow $hwnd;
sleep 1;
my $img = screenshot(hwnd => $hwnd, decor => 1);
die Imager->errstr unless $img;
$img->write(file => "$hwnd.$TYPE", type => $TYPE)
or die $img->errstr;
}
The code above will take separate screenshots for the overall IE window and the child window that holds the current tab. If you are only interested in top level IE windows, you'd want to use my @windows = FindWindowLike(0, 'Internet Explorer', '^IEFrame');
In addition, if you have opened an "InternetExplorer.Application" window using Win32::OLE
, you can access the object's Top, Height, and Width properties to determine its location and area. In addition, you can get its HWND so that you can set it as the foreground window.
#!/usr/bin/env perl
use strict; use warnings;
use Const::Fast;
use Imager;
use Imager::Screenshot qw( screenshot );
use Win32::GuiTest qw( SetForegroundWindow );
use Win32::OLE;
$Win32::OLE::Warn = 3;
const my $TYPE => 'bmp';
const my $READYSTATE_COMPLETE => 4;
my $browser = Win32::OLE->new("InternetExplorer.Application");
$browser->Navigate('http://www.example.com/');
sleep 1 while $browser->{ReadyState} != $READYSTATE_COMPLETE;
$browser->{Visible} = 1;
my $hwnd = $browser->{HWND};
SetForegroundWindow $hwnd;
sleep 1;
my $img = screenshot(hwnd => $hwnd, decor => 1) or die Imager->errstr;
my $title = $browser->{LocationName};
$browser->Quit;
$title =~ s/[^A-Za-z0-9_-]/-/g;
$img->write(file => "$title.$TYPE", type => $TYPE) or die $img->errstr;
Alternatively, using OLE events:
#!/usr/bin/env perl
use strict; use warnings;
use feature 'say';
use Const::Fast;
use Imager;
use Imager::Screenshot qw( screenshot );
use Win32::GuiTest qw( SetForegroundWindow );
use Win32::OLE qw(EVENTS valof);
$Win32::OLE::Warn = 3;
const my $TYPE => 'bmp';
const my $READYSTATE_COMPLETE => 4;
my ($URL) = @ARGV;
die "Need URL\n" unless defined $URL;
my $browser = Win32::OLE->new(
"InternetExplorer.Application", sub { $_[0]->Quit }
);
Win32::OLE->WithEvents($browser, \&Event, 'DWebBrowserEvents2');
$browser->{Visible} = 1;
$browser->Navigate2($URL);
Win32::OLE->MessageLoop;
Win32::OLE->SpinMessageLoop;
$browser->Quit;
sleep 3;
sub Event {
my ($browser, $event, @argv) = @_;
say $event;
if ($event eq 'DocumentComplete') {
$browser->{ReadyState} == $READYSTATE_COMPLETE
or return;
my $hwnd = $browser->{HWND};
SetForegroundWindow $hwnd;
my $img = screenshot(hwnd => $hwnd, decor => 1)
or die Imager->errstr;
my $url = valof( $argv[1] );
$url =~ s{^https?://}{};
$url =~ s{[^A-Za-z0-9_-]}{-}g;
$img->write(file => "$url.$TYPE", type => $TYPE)
or die $img->errstr;
Win32::OLE->QuitMessageLoop;
}
return;
}
Imager::Screenshot
will capture a window given a window handle. If you can find another module to get the window handle from the window title (or other identifying information) you should be able to capture just the window you want.screenshot(hwnd => 'active')
so if you can ensure the browser window is the active window you should be able to capture it.