Skip to content

pokemonish/chrome_remote_interface_python

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

68 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

chrome-remote-interface-python

Chrome Debugging Protocol interface that helps to instrument Chrome (or any other suitable implementation) by providing a simple abstraction of commands and notifications using a straightforward Python API.

This module is one of the many third-party protocol clients.

It is only for Python 3.5 for now

Sample API usage

The following snippet loads https://github.com and prints every response body length:

import asyncio
import chrome_remote_interface

if __name__ == '__main__':
    class callbacks:
        async def start(tabs):
            await tabs.add()
        async def tab_start(tabs, tab):
            await tab.Page.enable()
            await tab.Network.enable()
            await tab.Page.navigate(url='http://github.com')
        async def network__loading_finished(tabs, tab, requestId, **kwargs):
            try:
                body = tabs.helpers.old_helpers.unpack_response_body(await tab.Network.get_response_body(requestId=requestId))
                print('body length:', len(body))
            except tabs.FailResponse as e:
                print('fail:', e)
        async def page__frame_stopped_loading(tabs, tab, **kwargs):
            print('finish')
            tabs.terminate()
        async def any(tabs, tab, callback_name, parameters):
            pass
            # print('Unknown event fired', callback_name)

    asyncio.get_event_loop().run_until_complete(chrome_remote_interface.Tabs.run('localhost', 9222, callbacks))

We use these types of callbacks:

  • start(tabs) - fired on the start.
  • tab_start(tabs, tab, manual) - fired on tab create.
  • network__response_received(tabs, tab, **kwargs) - callback for chrome Network.responseReceived event.
  • any(tabs, tab, callback_name, parameters) - fallback which fired when there is no callback found.
  • tab_close(tabs, tab) - fired when tab is closed
  • tab_suicide(tabs, tab) - fired when tab is closed without your wish (and socket too)
  • close(tabs) - fired when all tabs are closed

We can add tab using method tabs.add() and remove it with tabs[n].remove() or tab.remove().

Each method can throw FailReponse exception when something goes wrong.

You can terminate your programm by calling tabs.terminate().

Installation

git clone https://github.com/wasiher/chrome-remote-interface-python.git
python3 setup.py install

Setup (all description from here)

An instance of either Chrome itself or another implementation needs to be running on a known port in order to use this module (defaults to localhost:9222).

Chrome/Chromium

Desktop

Start Chrome with the --remote-debugging-port option, for example:

google-chrome --remote-debugging-port=9222
Headless

Since version 57, additionally use the --headless option, for example:

google-chrome --headless --remote-debugging-port=9222

Please note that currently the DevTools methods are not properly supported in headless mode; use the Target domain instead. See #83 and #84 for more information.

Android

Plug the device and enable the port forwarding, for example:

adb forward tcp:9222 localabstract:chrome_devtools_remote
WebView

In order to be inspectable, a WebView must be configured for debugging and the corresponding process ID must be known. There are several ways to obtain it, for example:

adb shell grep -a webview_devtools_remote /proc/net/unix

Finally, port forwarding can be enabled as follows:

adb forward tcp:9222 localabstract:webview_devtools_remote_<pid>

Edge

Install and run the Edge Diagnostics Adapter.

Node.js

Start Node.js with the --inspect option, for example:

node --inspect=9222 script.js

Safari (iOS)

Install and run the iOS WebKit Debug Proxy.

Chrome Debugging Protocol versions

You can update it using this way (It will be downloaded automatically first time)

import chrome_remote_interface
chrome_remote_interface.Protocol.update_protocol()

Protocols are loaded from here and here

Contributors

Resources

About

Chrome Debugging Protocol interface for Python

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages