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

use Log::Log4perl qw(:easy); use WWW::Mechanize::Chrome; Log::Log4perl->easy_init($ERROR); # Set priority of root logger to ER +ROR my $mech = WWW::Mechanize::Chrome->new( headless => 0, incognito => 0, ); my $png = $mech->content_as_png(); exit;
The above opens two (2) chrome windows, one is in incognito mode and one is not.

. This is on a linux system (gentoo) running gnome (kernel 5.4.8)

. With chromium 83.0.4103.61

This seems like a bug, but I want some feedback before posting a bug, and also a way around it (workaround)... I need and expect a single chrome window in non-incognito mode (account mode), so I can save and restore cookies.

Thanks for your help!

Replies are listed 'Best First'.
Re: WWW:Mechanize::Chrome incognito
by Corion (Patriarch) on May 23, 2020 at 08:28 UTC

    Yes - currently, this is broken. Chrome always opens two windows currently if you display a window.

      Thanks for the quick reply! Is this a WMC bug, or a chrome bug? How do I get WMC to act on the non-incognito window? Are you saying if I open it headless it will work as expected? Please elaborate...
Re: WWW:Mechanize::Chrome incognito
by Discipulus (Canon) on May 23, 2020 at 11:00 UTC
    Hello tuneroaster and weclome to the monastery!

    incognito => 0 option has be proven to be currently broken as Corion said (he is the author an, yes! he will fix it ;) also in a very recent post by me: some doubts on my first steps with WWW::Mechanize::Chrome and remember Super Search is our friend!

    > please elaborate

    If the chrome browser was started without options atm incognito will be always be the mode. But if you start chrome with the option --remote-debugging-port=9222 or manually or in your perl program, then you'll be able to connect to an existing instance and to an existing tab. I was prepaering the following example that shows what I mean:

    use strict; use warnings; use Log::Log4perl qw(:easy); use WWW::Mechanize::Chrome; Log::Log4perl->easy_init($ERROR); # Set priority of root logger to ER +ROR my $mech = connect_to_an_existing_tab(); unless ( $mech ){ my $chrome_exe = choose_chrome_executable(); start_new_instance( $chrome_exe ) } print "broswer started or connected succesfully, listening on remote-d +ebugging-port ". ${$mech->target()}{transport}{port}, " status ", ${$mech->target()}{transport}{is_connected} ? "connected" : + "not connected","\n"; sleep 3; # go from Monastery Gates to Super Search $mech->click ( { selector => '#titlebar-top tbody tr td.monktitleb +ar ul li:nth-child(16) a'} ); # the super search form $mech->form_number( 2 ); # titles containing text field $mech->field( 'HIT' => 'WWW::Mechanize::Chrome'); # not working: SUPPORT NEEDED HERE ;) # # $mech->untick( 're', 'S' ); # $mech->tick( 're', 'N' ); # submit $mech->click( { selector => '#id-3989 center table tbody tr td.main_co +ntent form p:nth-child(17) input[type=submit]'} ); # ATTENTION: autoclose was fixed recently: # if ( $WWW::Mechanize::Chrome::VERSION < 0.56){ print "Press ENTER to terminte ( autoclose => 0 fixed in version 0 +.56 of the module)".."\n"; <STDIN>; } sub choose_chrome_executable{ my( $chrome, $diagnosis ) = WWW::Mechanize::Chrome->find_executabl +e(); if ( $diagnosis ){ print "problem finding chrome executable: $diagnosis\n"; } print "Should I use ",( $chrome ? "[$chrome]" : "-NOT FOUND-" ), " + or another executable (put full path or leave blank for default)\n"; my $chrome_path = <STDIN>; chomp $chrome_path; $chrome_path = -e $chrome_path ? $chrome_path : $chrome; return $chrome_path; } sub connect_to_an_existing_tab{ print "I have to use an existing chrome tab by title( for example +PerlMonks )? leave it blank to open a new browser instance\n"; my $tab_title = <STDIN>; chomp $tab_title; if ( $tab_title ){ my $mech; { local $@; eval{ $mech = WWW::Mechanize::Chrome->new( mute_audio => 0, autoclose => 0, autodie => 0, incognito => 0, tab => qr/$tab_title/, ); }; if ( $@ ){ print "problem connecting to an already open tab in a +running instance of chrome:\n$@\n\n". "close your running chrome instance and anothe +r one will be started with right configuration\n". "Press ENTER to continue..\n"; <STDIN>; return 0; } return $mech; } } else { return 0 } } sub start_new_instance{ my $chrome_exe = shift; print "I'll now open https://www.perlmonks.org (will be in incogn +ito mode despite incognito => 0)\n"; my $url = 'https://www.perlmonks.org'; $mech = WWW::Mechanize::Chrome->new( mute_audio => 0, autoclose => 0, autodie => 0, incognito => 0, launch_exe => $chrome_exe, launch_arg => [ "--remote-debugging-port=9222" ] ); $mech->get( $url ); }

    L*

    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

      STOP THE PRESSES! IT WORKED:

      From the shell, I failed to start chromium with --remote-debugging-port=9222

      So I guess, in a script, I can spawn chromium headless, then run the per script and attach to it! It just might work! THANKS!

      ---------------------------------------

      OK, my code is now:
      use Log::Log4perl qw(:easy); use WWW::Mechanize::Chrome; Log::Log4perl->easy_init($ERROR); my $mech = WWW::Mechanize::Chrome->new( headless => 0, # start_url => 'https://grocery.walmart.com/orders', data_directory => '/home/rwk/chrome-session-for-walmart', incognito => 0, launch_exe => '/usr/bin/chromium', javascript => 1, launch_arg => [ "--remote-debugging-port=9222" ], tab => qr/Walmart Grocery/, ); my $txt = $mech->content; open($f, '>', '/tmp/out.html'); print $f $txt; close $f;
      I start the browser manually and load https://grocery.walmart.com/orders

      It correctly logs in and display my order page.

      The page Title shows "Walmart Grocery".

      I run my script above and get:

      error when connectingInternal Exception at /usr/local/lib64/perl5/5.30 +.1/WWW/Mechanize/Chrome.pm line 993.
      Please advise...
      Thanks again! You say:

      "if you start chrome with the option --remote-debugging-port=9222 or manually or in your perl program, then you'll be able to connect to an existing instance and to an existing tab"

      Does this mean my task cannot be scripted? I.e, I must run chrome from a shell, then attach to it?

      Do you also have to set "port => 9222" in the perl constructor?

      I tried your script, got the fillowing:
      I'll now open https://www.perlmonks.org (will be in incognito mode de +spite incognito => 0) error when connectingInternal Exception at /usr/local/lib64/perl5/5.30 +.1/WWW/Mechanize/Chrome.pm line 993. at /usr/local/lib64/perl5/5.30.1/Future.pm line 899. Future::result(Future=HASH(0x55e2e584d3c0)) called at /usr/local/l +ib64/perl5/5.30.1/Future.pm line 926 Future::get(Future=HASH(0x55e2e584d3c0)) called at /usr/local/lib6 +4/perl5/5.30.1/WWW/Mechanize/Chrome.pm line 993 WWW::Mechanize::Chrome::_connect(WWW::Mechanize::Chrome=HASH(0x55e +2e4ab30d0), "mute_audio", 0, "pid", 17465, "launch_exe", "/usr/bin/ch +romium", "writer_fh", ...) called at /usr/local/lib64/perl5/5.30.1/WW +W/Mechanize/Chrome.pm line 966 WWW::Mechanize::Chrome::new("WWW::Mechanize::Chrome", "mute_audio" +, 0, "autoclose", 0, "autodie", 0, "incognito", ...) called at /home/ +rwk/mech-sample.pl line 99 main::start_new_instance("/usr/bin/chromium") called at /home/rwk/ +mech-sample.pl line 14

      I am using WMC 0.56

      BTW, Thanks for all your work on this!!!! WMC appears to be VERY useful (once I get it working).
Re: WWW:Mechanize::Chrome incognito
by Corion (Patriarch) on May 24, 2020 at 17:36 UTC

    The just released version 0.58 should use the already open window unless you specify separate_session.

      I just tried it (0.58) without "tab" and without external launch of chrome and it now works (with and without headless)! Cookies and all! Thanks and GOOD WORK!!! Outstandingly useful module!
      Strangely, 0.58 displays on cpan, but when I install (using linux cpan) version 0.57 installs. Is there a lag somewhere?

      Does this mean I no longer need to specify the "tab" option? I do still need to spawn chrome from a shell, right?

        "Strangely, 0.58 displays on cpan, but when I install (using linux cpan) version 0.57 installs. Is there a lag somewhere? "

        Perhaps the mirror you are using is not up to date:

        cpanm https://cpan.metacpan.org/authors/id/C/CO/CORION/WWW-Mechanize-C +hrome-0.58.tar.gz