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

Win32::OLE and IE automation

I’m working on some potential enhancements of Win32::IE::Mechanize and have encountered a problem with popups (NewWindow3/2). The following code implements what several samples on the web use to block popups by setting ‘cancel’ true (see http://msdn.microsoft.com/workshop/browser/webbrowser/reference/events/newwindow2.asp). The code successfully navigates several windows that DO NOT have popups but when it encounters a popup it crashes. I’ve been working on it for most of the day and am out of ideas. Any monks that can provide a clue? Additionally, if anyone can run it in a different environment, I'd appreciate it. It could be something about my machine. Note, you MUST DISABLE ALL YOUR POPUP BLOCKERS. Thanks.
#!/usr/bin/perl -w # # use local version of mech with alternate page completion logic # use strict; use Time::HiRes qw(gettimeofday); use URI; use Win32::OLE qw( EVENTS in with valof ); use Win32::OLE::Variant; my $t_start; my $tend = gettimeofday; my $url; my $urlCounter= 0; my $timeTestStart = time(); my $t_now; my $t_last_event; my $dl_tot = 0; my $dl_cnt = 0; my $timedelay = 5; my $timeout = 60; $|=1; my $ie = Win32::OLE->new( 'InternetExplorer.Application' ) or die( "Cannot create an InternetExplorer.Application" ); $ie->{menubar} = 1; $ie->{toolbar} = 1; $ie->{statusbar} = 1; $ie->{visible} = 1; # give IE a chance to get itself established print "IE should be visible\n"; $ie->navigate('about:blank'); sleep 10; Win32::OLE->WithEvents( $ie, \&win32_ie_events, "DWebBrowserEvents2" ) +; my @urls = qw( http://www.cnn.com http://www.whitehouse.gov http://www.popuptest.com/popuptest12.html http://www.popuptest.com/popuptest1.html http://www.instantattention.com/?aid=1589 ); foreach $url (@urls) { $url =~ s/\s//; if( $url =~ /^#/) { next; } # do not nav to pdf files if( $url =~ /^$/) { next; } $urlCounter++; my $elapsed = time() - $timeTestStart; my @xtime = gmtime($elapsed); print "\n\n"; print localtime(time) . " elapsed " . $xtime[2] . ":" . $xtime[1] +. ":" . $xtime[0] . "\n"; print "url $urlCounter $url\n"; $dl_tot = 0; $dl_cnt = 0; $t_start = $t_last_event = gettimeofday(); $ie->navigate($url); while (1) { Win32::OLE->SpinMessageLoop; # get current time $t_now = gettimeofday(); # check if navigation is complete if((($t_now - $t_last_event) > $timedelay) && # no events f +or a bit ($ie->ReadyState == 4) && # browser says + it's ready $dl_tot && # we've had so +me downloads ($dl_cnt == 0)) { # we've had eq +ual number of download completes print "done ok\n"; last; # we're done } # check for timeout if(( $t_now - $t_start ) > $timeout ) { # temp code, this hangs sometimes, need x19 style stuff, s +ometimes this seems to hang!! print "timeout\n"; sleep 5; last; } } my $seconds = $t_last_event - $t_start; print "Returned $seconds\n"; } $ie->close; exit; sub win32_ie_events { my( $agent, $event, @args ) = @_; $t_last_event = gettimeofday(); CASE: { $event eq 'DownloadBegin' and do { $dl_cnt++; last CASE; }; $event eq 'DownloadComplete' and do { $dl_cnt--; $dl_tot++; last CASE; }; $event eq 'NewWindow2' and do { print "NewWindow2 kill popup\n"; $args[1]->Put( 1 ); print "cancel[" .$args[1]->Value() . "]\n"; }; $event eq 'NewWindow3' and do { print "NewWindow3 kill popup\n"; print "$args[2], $args[3], $args[4]\n"; $args[1]->Put( 1 ); print "cancel[" .$args[1]->Value() . "]\n"; } } my $te = sprintf '%6.2f', $t_last_event - $t_start; print "$te $dl_cnt $dl_tot [$event]\n"; }

Edit: g0n - linkified link & readmore tags

Replies are listed 'Best First'.
Re: Win32::OLE and IE automation
by puff (Beadle) on Nov 29, 2005 at 15:07 UTC
    It turns out that there is a bug in the current version of Win32::OLE that causes the crash. Jan Dubois has sent a patch and I expect there will be a new version available shortly. Thanks very much Jan!

    Based on my testing other posted popup blockers (http://www.perlmonks.org/index.pl?node_id=273090, and http://www.roth.net/perl/scripts/scripts.asp?IEEVENTS.PL) that use Win32::OLE also fail. I believe that Jan's patch addresses them all. If this is not the case, I will post accordingly.