2

I am trying to use WWW::Mechanize::Chrome to fill in fields in the currently active tab in Chrome. The general goal is to automate some of the typing involved in making bank transfers so opening a new session and navigation to pages are no-go.

This is where I am at - to test a minimal level of control on the current tab:

use Log::Log4perl qw(:easy);
use WWW::Mechanize::Chrome;
 
Log::Log4perl->easy_init($ERROR);

my $mech = WWW::Mechanize::Chrome->new(
                       launch_exe => '/usr/bin/google-chrome',
                       launch_arg => [ "--remote-debugging-port=9223" ],
                       autoclose => 0,
                       port => 9223,
                       tab => 'current',
                      );

$mech->get('https://www.reddit.com/r/perl');
$mech->eval_in_page('alert("Hello Chrome")');

my $png = $mech->content_as_png();

with Chrome started like this:

/usr/bin/google-chrome --remote-debugging-port=9223

However what this actually does (on the browser side) is:

  • open a new tab
  • no navigation to reddit
  • no alert

and on the command line it outputs this:

Can't connect without knowing the port?! 0 at ~/perl5/perlbrew/perls/perl-5.30.2/lib/site_perl/5.30.2/Chrome/DevToolsProtocol.pm line 317.

If Chromium is not running, the code works well (opens Chromium, navigates to reddit, alerts).

The docs say "Setting tab to current will use the current, active tab instead - but it does not seem to work.

What is it that I'm doing wrong?

simone
  • 4,667
  • 4
  • 25
  • 47
  • When I test this script, the constructor `WWW::Mechanize::Chrome->new( ...)` aborts with error *"Can't connect without knowing the port?! 0 at /home/hakon/perlbrew/perls/perl-5.30.0/lib/site_perl/5.30.0/Chrome/DevToolsProtocol.pm line 317."* – Håkon Hægland Apr 13 '21 at 09:48
  • @HåkonHægland - correct, I added that to the question, and specified that my info referred to what happens in the browser – simone Apr 13 '21 at 13:32
  • Added [bug report](https://github.com/Corion/WWW-Mechanize-Chrome/issues/56) – Håkon Hægland Apr 13 '21 at 13:51

1 Answers1

1

I had a quick chat with the author of WWW::Mechanize::Chrome off-band. He says you need to set the remote debugging port when you launch the Chrome instance you want to attach to. It does this automatically when it opens a new one.

--remote-debugging-port 9222

Here is some information about turning it on all the time.

simbabque
  • 53,749
  • 8
  • 73
  • 136
  • I tried that, but it doesn't seem to work. I've updated the question accordingly. Appreciate the insider knowledge though – simone Apr 13 '21 at 13:37