in reply to WWW::Mechanize::Chrome - how to deal with new windows/tabs

If you have the title of the new tab, you can create a new WWW::Mechanize::Chrome object and connect it to that new tab:

my $chrome = $mech->driver; my @tabs = $chrome->getTargets()->get; # Assume that the first tab is the one we want: my $target_tab = $tabs[ 0 ]; if( ! $target_tab->{targetId}) { die "This Chrome doesn't want more than one debugger connectio +n"; } else { $chrome->connect(tab => $target_tab)->get(); }; # Now, the WWW::Mechanize::Chrome object is mostly connected to th +e new/other tab

Maybe you want to just create a separate, fresh object?

my $mech_tab = WWW::Mechanize::Chrome->new( tab => $new_tab_title );

Using multiple tabs or windows is a use case I haven't encountered that much, so there is very little existing support for it.

Replies are listed 'Best First'.
Re^2: WWW::Mechanize::Chrome - how to deal with new windows/tabs
by mscha (Acolyte) on Sep 06, 2020 at 20:07 UTC

    Thanks, your first example does the trick for me! (The second could be useful as well, depending on the use case.)

    $mech->get('https://www.mscha.org/knmi/summer.cgi?month=-1'); $mech->follow_link(text=>'2020'); # opens a new window my $chrome = $mech->driver; my @tabs = $chrome->getTargets()->get; my ($new_tab) = grep { $_->{url} =~ m{graph} } @tabs; $chrome->connect(tab=>$new_tab)->get; my $png = $mech->content_as_png;

    I don't have control over the site I want to eventually use this for, and it opens new tabs all over the place. So I do need to be able to do this.
    It might be useful for other people to include some documentation or an example that shows tab switching.