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

Hello Monks!

I have a Centos 8 VM running on my local machine with a fresh install of Chrome 87.0.4270.0 and WWW::Mechanize::Chrome installed to version 0.60. In trying the dump-links.pl sample project I get this error:

Can't connect without knowing the port?! 0 at /usr/local/share/perl5/C +hrome/DevToolsProtocol.pm line 316.

I tried setting the port as per the docs:

my $mech = WWW::Mechanize::Chrome->new(port => 9222);
Same error. I followed this earlier post as well but no luck: https://www.perlmonks.org/?node_id=1203079

From the $TRACE it appears the port is not being set:

2020/09/26 13:02:28 Spawning $VAR1 = [ '/usr/bin/google-chrome', '--remote-debugging-port=0', '--enable-automation', '--no-first-run', '--mute-audio', '--no-zygote', '--no-sandbox', '--safebrowsing-disable-auto-update', '--disable-background-networking', '--disable-breakpad', '--disable-client-side-phishing-detection', '--disable-component-update', '--disable-hang-monitor', '--disable-prompt-on-repost', '--disable-sync', '--disable-translate', '--disable-web-resources', '--disable-default-apps', '--disable-infobars', '--disable-popup-blocking', '--disable-gpu', '--disable-save-password-bubble', 'about:blank' ]; 2020/09/26 13:02:28 Spawned child as 7191 Can't connect without knowing the port?! 0 at /usr/local/share/perl5/C +hrome/DevToolsProtocol.pm line 316.
Any thoughts on this would be much appreciated!! Thanks! Ed

Replies are listed 'Best First'.
Re: WWW::Mechanize::Chrome Can't Connect without knowing the port?!
by Corion (Patriarch) on Sep 26, 2020 at 20:40 UTC

    Oops! This seems to be a regression that nobody found yet!

    As a workaround, you can use

    WWW_MECHANIZE_CHROME_CONNECTION_STYLE=pipe perl -Ilib -MWWW::Mechanize +::Chrome -wle 'my $mech = WWW::Mechanize::Chrome::->new()'

    ... or, for a script

    #!perl use strict; use WWW::Mechanize::Chrome; $ENV{WWW_MECHANIZE_CHROME_CONNECTION_STYLE}='pipe'; my $mech = WWW::Mechanize::Chrome::->new(); ...

      Thank you for the quick reply!!!

      Does that only work headless? When I insert that code into dump-links.pl I get this:

      2020/09/26 14:01:35 Spawning $VAR1 = [ '/usr/bin/google-chrome', '--remote-debugging-pipe', '--enable-automation', '--no-first-run', '--mute-audio', '--no-zygote', '--no-sandbox', '--safebrowsing-disable-auto-update', '--disable-background-networking', '--disable-breakpad', '--disable-client-side-phishing-detection', '--disable-component-update', '--disable-hang-monitor', '--disable-prompt-on-repost', '--disable-sync', '--disable-translate', '--disable-web-resources', '--disable-default-apps', '--disable-infobars', '--disable-popup-blocking', '--disable-gpu', '--disable-save-password-bubble', 'about:blank' ]; 2020/09/26 14:01:35 Spawned child as 3455 [3455:3455:0926/140135.852054:ERROR:browser_main_loop.cc(1434)] Unable + to open X display. 2020/09/26 14:01:35 Sent message $VAR1 = '{"id":1,"method":"Target.get +Targets","params":{}}'; $VAR1 = [ bless( { 'read_len' => 8192, 'reader' => '_sysread', 'cb_r' => sub { "DUMMY" }, 'want_readready' => 1, 'close_on_read_eof' => 1, 'handle_closing' => undef, 'write_handle' => \*{'WWW::Mechanize::Chrome::$pare +nt{...}'}, 'want_writeready' => 1, 'read_handle' => \*{'WWW::Mechanize::Chrome::$paren +t{...}'}, 'want' => 9, 'on_read' => sub { "DUMMY" }, 'write_eof' => 1, 'writequeue' => [ bless( [ '{"id":1,"method":"Targe +t.getTargets","params":{}}', 8192, undef, undef, undef, 0 ], 'IO::Async::Stream::Wri +ter' ) ], 'write_len' => 8192, 'writeable' => 1, 'readqueue' => [], 'writer' => '_syswrite', 'cb_w' => sub { "DUMMY" }, 'on_write_error' => sub { "DUMMY" }, 'IO_Async_Notifier__loop' => bless( { 'os' => {}, 'deferrals' = +> [], 'sigattaches' + => {}, 'timequeue' = +> undef, 'pollevents' +=> { + '7' => 16, + '6' => 12 + }, 'notifiers' = +> { + '94094341331112' => $VAR1->[0] + }, 'childwatches +' => {}, 'pollmask' => + { + '6' => 4, + '7' => 1 + }, 'childmanager +' => undef, 'threadwatche +s' => {}, 'iowatches' = +> { + '6' => [ + $VAR1->[0]{'write_handle'}, + undef, + $VAR1->[0]{'cb_w'} + ], + '7' => [ + $VAR1->[0]{'read_handle'}, + $VAR1->[0]{'cb_r'} + ] + }, 'poll' => und +ef, 'old_timer' = +> '' }, 'IO::Async:: +Loop::Poll' ), 'readbuff' => '' }, 'IO::Async::Stream' ), 'Broken pipe' ];
        [3455:3455:0926/140135.852054:ERROR:browser_main_loop.cc(1434)] Unable + to open X display.

        So maybe your Chrome process can't connect to your display server. This would explain why it works headless but not when trying with a display. I don't know why Chrome can't connect to a display - are you launching the process as a different user or in a shell without the DISPLAY variable set?