2

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

Bijan
  • 7,737
  • 18
  • 89
  • 149

2 Answers2

2

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});
    }
}
zdim
  • 64,580
  • 5
  • 52
  • 81
1

More concisely:

$ff->closeTab($_->{tab}) for grep { $_->{title} eq 'TITLE HERE' } $ff->openTabs;
Borodin
  • 126,100
  • 9
  • 70
  • 144