根据标题关闭标签

时间:2016-04-06 05:03:23

标签: perl firefox mechanize www-mechanize www-mechanize-firefox

如何使用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()的文档只是说“关闭给定标签”,但没有关于给定标签的信息

2 个答案:

答案 0 :(得分:2)

$ff->closeTab($tab->{tab})。例如,请参阅Cookbook。一个完整的计划:

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});
    }
}

答案 1 :(得分:1)

更简洁:

$ff->closeTab($_->{tab}) for grep { $_->{title} eq 'TITLE HERE' } $ff->openTabs;