9

Win32 :: IEAutomation will not click links when IE & gt; = 9

 2 years ago
source link: https://www.codesd.com/item/win32-ieautomation-will-not-click-links-when-ie-9.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Win32 :: IEAutomation will not click links when IE & gt; = 9

advertisements

On a modern setup (Windows 7 64-bit, IE 11, ActiveState Perl 5.16 64-bit), the Click method in Win32::IEAutomation (v0.5) does not seem to work. Here is an example, lightly adapted from the documentation:

use Win32::IEAutomation;
my $ie = Win32::IEAutomation->new( visible => 1);
$ie->gotoURL('http://www.google.com');
$ie->getLink('linktext:', "About")->Click;

At this point, I should see the "About" page in IE. But I still see Google's home page in IE, and I cannot use the Content method in Win32::IEAutomation to get the source of the "About" page.

I have the same problem on an older setup (Vista SP2 64-bit, IE 9, ActiveState Perl 5.10.1). But the problem does not arise when I use a similar setup with IE8 instead of IE9. The problem thus seems to lie with a difference between IE8 and subsequent IE versions.

Is there anything that I can do to get the example script working with more recent versions of IE?


Win32::IEAutomation is a thin wrapper around the various interfaces exposed by InternetExplorer.Application and MSHTML.

Therefore, I tried to replicate the problem by writing a script to do the navigation without using Win32::IEAutomation. Using the click method on a link did not initiate navigation whereas passing its href to Navigate2 did.

The click method "Simulates a click by causing the HTMLFrameSiteEvents::onclick event to fire," meaning any onClick handlers defined on the page will be involved. I am not sure why specifically navigation is not being initiated.

However, the problem is not specific to Google's home page: I tried it with example.com, and invoking the click method on a link on that page did not initiate navigation either.

Here is the script I used as a testbed:

#!/usr/bin/env perl

use strict;
use warnings;
use feature 'say';

use Win32::OLE qw(EVENTS in valof);
$Win32::OLE::Warn = 3;

my $url = 'https://www.google.com/';

my %event_handler = (
    DocumentComplete => \&onDocumentComplete,
);

my %page_handler = (
    'https://www.google.com/'
        => \&onPageGoogleHome,
    'https://www.google.com/intl/en/about/'
        => \&onPageGoogleAbout,
);

my $ie = Win32::OLE->new(
    "InternetExplorer.Application", sub { $_[0]->Quit }
);

Win32::OLE->WithEvents($ie, \&Event, 'DWebBrowserEvents2');

$ie->{Visible} = 1;
$ie->Navigate2($url);

Win32::OLE->MessageLoop;
Win32::OLE->SpinMessageLoop;

$ie->Quit;

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

    if (exists $event_handler{$event}) {
        $event_handler{$event}->($ie, \@argv);
    }
    else {
        # unhandled event
    }
    return;
}

sub onDocumentComplete {
    my ($ie, $argv) = @_;
    my $url = valof($argv->[-1]);
    if (exists $page_handler{$url}) {
        $page_handler{$url}->($ie, $argv);
    }
    else {
        # unhandled page
    }
    return;
}

sub onPageGoogleHome {
    my ($ie, $argv) = @_;
    say "We are on Google's home page";
    my $links = $ie->Document->links;
    my $about_link;
    for my $link (in $links) {
        if ($link->innerText eq 'About') {
            say "Found 'About' link";
            $about_link = $link;
            last;
        }
    }
    if ($about_link) {
        # Doesn't work:
        # $about_link->click;

        $ie->Navigate2($about_link->href);
    }
    return;
}

sub onPageGoogleAbout {
    my ($ie, $argv) = @_;
    say "Yay, we are on the about page!";
    Win32::OLE->QuitMessageLoop;
    return;
}

Version information:

This is perl 5, version 19, subversion 12 (v5.19.12) built for MSWin32-x64-multi-thread

Internet Explorer 11

Windows 8.1 Pro 64-bit


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK