Chrome::DevToolsProtocol - asynchronous dispatcher for the DevTools protocol


    # Usually, WWW::Mechanize::Chrome automatically creates a driver for you
    my $driver = Chrome::DevToolsProtocol->new(
        port => 9222,
        host => '',
        auto_close => 0,
        error_handler => sub {
            # Reraise the error
            croak $_[1]
    $driver->connect( new_tab => 1 )->get


->new( %args )

    my $driver = Chrome::DevToolsProtocol->new(
        port => 9222,
        host => '',
        auto_close => 0,
        error_handler => sub {
            # Reraise the error
            croak $_[1]

These members can mostly be set through the constructor arguments:


The hostname to connect to


The port to connect to


The JSON decoder used


The Future::HTTP instance to talk to the Chrome DevTools


Which tab to reuse (if any)


A premade Log::Log4perl object to act as logger


A callback invoked for every message


The event-loop specific transport backend


    my $f = $driver->future();

Returns a backend-specific generic future


    my $url = $driver->endpoint();

Returns the URL endpoint to talk to for the connected tab


Filehandle where all communications will be logged to, one line per message/response. Each line will be of the format

  { type: "message", payload: { ... } }
  { type: "reply", payload: { ... } }
  { type: "event", payload: { ... } }

The filehandle must be opened as :raw , as UTF-8 encoded bytes will be written to it.


    my $l = $driver->add_listener(
        sub {
            warn "The DOMContent event was fired";

    # ...

    undef $l; # stop listening

Adds a callback for the given event name. The callback will be removed once the return value goes out of scope.



Explicitly remove a listener.


    $driver->log('debug', "Warbling doodads", { doodad => 'this' } );

Log a message


    my $f = $driver->connect()->get;

Asynchronously connect to the Chrome browser, returning a Future.



Shut down the connection to Chrome



Sleep for the amount of seconds in an event-loop compatible way


    my $f = $driver->one_shot('Page.domContentEventFired')->get;

Returns a future that resolves when the event is received


    my $data = $driver->json_get( 'version' )->get;

Requests an URL and returns decoded JSON from the future


      accept => JSON::true,

Sends a JSON packet to the remote end


  my $future = $chrome->send_message('DOM.querySelectorAll',
      selector => 'p',
      nodeId => $node,
  my $nodes = $future->get;

This function expects a response. The future will not be resolved until Chrome has sent a response to this query.




    my $result = $chrome->eval('2+2');

Evaluates a Javascript string and returns the result.


    print $chrome->version_info->get->{"Protocol-Version"};

Returns the implemented ChromeDevTooslProtocol protocol version.


    print $chrome->protocol_version->get;


Returns information about the Chrome instance we are connected to.


    my $schema = $chrome->get_domains->get;

Returns the topics of this Chrome DevToolsProtocol implementation.


  my @tabs = $chrome->list_tabs->get();


    my $new_tab = $chrome->new_tab('')->get;


    $chrome->activate_tab( $tab )->get

Brings the tab to the foreground of the application


    $chrome->close_tab( $tab )->get

Closes the tab


    my @targets = $chrome->getTargets->get;

Gets the list of available targets


    my $info = $chrome->getTargetInfo( $targetId )->get;
    print $info->{title};

Returns information about the current target


    my $targetId = $chrome->createTarget(
        url => 'about:blank',
        width => 1280,
        height => 800,
        newWindow => JSON::false,
        background => JSON::false,
    print $targetId;

Creates a new target, optionally in a new window


    my $sessionId = $chrome->attachToTarget(
        targetId => $targetId,
    print $sessionId;

Attaches to the target so we receive events generated by the target


    my $targetId = $chrome->closeTarget(
        targetId => $targetId

Creates a new target


    my $info = $chrome->getWindowForTarget( $targetId )->get;
    print $info->{windowId};

Returns information about the window of the current target


    my @browserContextIds = $chrome->getBrowserContexts->get;

Gets the list of available browser contexts. These are separate sets of user cookies etc.


The inofficial Chrome debugger API documentation at

Chrome DevTools at


The public repository of this module is


The public support forum of this module is


Please report bugs in this module via the Github bug queue at


Max Maischein


Copyright 2010-2021 by Max Maischein


This module is released under the same terms as Perl itself.