Using Win32::OLE with events to capture Internet Explorer screenshots with Perl

Another post inspired by a Stackoverflow question.

The OP wanted to know how to capture a screen shot of a specific Internet Explorer window using Perl’s Imager::Screenshot which is a very useful module I did not know about until I came across this question.

I give three methods in my answer. The first one uses Win32::GuiTest to find all sorts of browser windows and take shots of all of them. The second one uses Win32::OLE to create an InternetExplorer.Application instance to navigate to a specific URL and uses a busy loop to wait for the document to be loaded.

The third one utilizes events so as to avoid busy loops. It’s a little tricky because it seems like if the document contains dynamically loaded elements, the first DocumentComplete event may be fired even if there are still elements to be loaded.

The following script demonstrates the use of event support with Win32::OLE:

#!/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';

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;




sleep 3;

sub Event {
    my ($browser, $event, @argv) = @_;

    if ($event eq 'DocumentComplete') {
        sleep 1;
        $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;

    elsif ($event eq 'StatusTextChange') {
        say $browser->{StatusText};
    else {
        say $event;


We first create an instance of InternetExplorer.Application, make it visible so we can receive DocumentComplete events and enter the message loop.

In the event handler, every time we receive a DocumentComplete event, we sleep a little, and check if the ReadyState property indicates it’s safe to take a screenshot.

If so, we query the $browser object to find its window handle, bring that window to the foreground using Win32::GuiTest::SetForegroundWindow, and take the screenshot.

This is still not infallible as it is possible for something else to pop up and partially obscure the window between the SetForegroundWindow call and the screenshot being taken, but I am not sure what can be done with that.

Once the screenshot is taken, we quit the message loop. Win32::OLE documentation recommends invoking SpinMessageLoop and waiting a few seconds, so that explains the funky sleep at the end. Through trial and error, I found it gave more reliable results to invoke $browser->Quit after the SpinMessageLoop but before the sleep.

I tested the code on Windows XP SP3 with IE 8 using ActivePerl 5.14.2. Please let me know if it works properly on Vista and Windows 7 systems as well.