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


In reply to Win32::OLE and IE automation by puff

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.