如何使用WWW::Mechanize::Firefox
根据选项卡的标题关闭选项卡?
这是我目前拥有的:
my $ff = Firefox::Application->new();
my @tab_info = $ff->openTabs();
foreach my $tab (@tab_info) {
if($tab->{title} eq "TITLE HERE") {
$ff->closeTab($tab->{location});
}
}
closeTab()的文档只是说"关闭给定的选项卡",而没有关于给定选项卡是什么的信息
它是$ff->closeTab($tab->{tab})
。例如,请参阅食谱。一个完整的程序:
use WWW::Mechanize::Firefox;
my $ff = Firefox::Application->new();
my $title_to_close = "Title of the page to close ...";
# This will pull in all currently opened tabs
my @tabs = $ff->openTabs();
foreach my $tab (@tabs) {
if ($tab->{title} =~ /$title_to_close/) {
print "Close tab: $tab->{title}";
$ff->closeTab($tab->{tab});
}
}
更简洁地说:
$ff->closeTab($_->{tab}) for grep { $_->{title} eq 'TITLE HERE' } $ff->openTabs;