mscha has asked for the wisdom of the Perl Monks concerning the following question:

I'm starting to try out WWW::Mechanize::Chrome, and it looks pretty good!

However, I'm having trouble when following links that open a new window (window.open) or tab (target="_blank"). The new window/tab does open, but the original tab remains selected, and I can't figure out a way to select (or otherwise interact with) the new window/tab.

Any way to do this?

$mech->get('https://www.mscha.org/knmi/summer.cgi?month=-1'); $mech->follow_link(text=>'2020'); # opens a new window my $png = $mech->content_as_png;

$png now contains a screenshot of the original window, not the newly opened one. Any way to access the newly opened window?

Replies are listed 'Best First'.
Re: WWW::Mechanize::Chrome - how to deal with new windows/tabs
by Corion (Patriarch) on Sep 06, 2020 at 17:54 UTC

    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.

      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.

Re: WWW::Mechanize::Chrome - how to deal with new windows/tabs
by Anonymous Monk on Sep 06, 2020 at 15:36 UTC
      > https://metacpan.org/pod/WWW::Mechanize::Chrome#$mech-%3Etab

      Yes, I read the documentation. But I don't see how that helps me.

        >Access the tab hash of the Chrome::DevToolsProtocol::Target instance.

        Never used this before, but I am going to guess that this means $tag is a hash reference that contains all the opened tabs. Maybe explore the contents of $tag in the example.

        Why not?

Re: WWW::Mechanize::Chrome - how to deal with new windows/tabs
by Anonymous Monk on Sep 06, 2020 at 20:56 UTC
    To most "mechanizers," each tab is an independent browser. They interact with those browser instances and have no concept of "tabs" (or "windows").