I’m working on some enhancements to IE::Mechanize involving popups.  Several online examples suggest blocking popups from within the event routine with code like this

 

$args[1]->Put( 1 );

 

When I use this technique I get perl crashes (see test below)!  Can anyone point me to an example that works?

 

Test code

 

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

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)  &&  

            ($ie->ReadyState == 4) &&  

            $dl_tot &&    

            ($dl_cnt == 0)) {   

            print "done ok\n";

            last;      

        }

        # check for timeout

        if(( $t_now  - $t_start ) > $timeout ) {

            # temp code, this hangs sometimes, need x19 style stuff, sometimes 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";

}

 

_______________________________________________
Perl-Win32-Users mailing list
Perl-Win32-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to