Win32::IEAutomation 是InternetExplorer.Application 和MSHTML 公开的各种接口的薄包装。
因此,我尝试通过编写脚本来进行导航而不使用Win32::IEAutomation 来复制问题。在链接上使用click 方法不会启动导航,而将其href 传递给Navigate2 会启动导航。
click 方法“通过触发 HTMLFrameSiteEvents::onclick 事件来模拟点击”,这意味着将涉及页面上定义的任何 onClick 处理程序。我不确定为什么没有特别启动导航。
但是,问题并不特定于 Google 的主页:我尝试使用 example.com,在该页面上的链接上调用 click 方法也没有启动导航。
这是我用作测试平台的脚本:
#!/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;
}
版本信息:
这是为 MSWin32-x64-multi-thread 构建的 perl 5,版本 19,subversion 12 (v5.19.12)
Internet Explorer 11
Windows 8.1 专业版 64 位