perlfirefoxmechanizewww-mechanizewww-mechanize-firefox

Close Tab Based on Title


How would I close a tab based on it's title using WWW::Mechanize::Firefox?

Here is what I currently have:

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

The documentation for closeTab() just says 'Close the Given Tab' with no information on what the given tab is


Solution

  • It is $ff->closeTab($tab->{tab}). See the Cookbook, for example. A complete program:

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