https://github.com/fate0/pychrome
A Python Package for the Google Chrome Dev Protocol [threading base]
https://github.com/fate0/pychrome
chrome chrome-debugging-protocol chrome-devtools headless headless-chrome pychrome python
Last synced: 10 months ago
JSON representation
A Python Package for the Google Chrome Dev Protocol [threading base]
- Host: GitHub
- URL: https://github.com/fate0/pychrome
- Owner: fate0
- License: other
- Created: 2017-07-25T11:01:29.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2024-06-17T16:55:43.000Z (over 1 year ago)
- Last Synced: 2025-04-03T19:11:15.722Z (10 months ago)
- Topics: chrome, chrome-debugging-protocol, chrome-devtools, headless, headless-chrome, pychrome, python
- Language: Python
- Homepage:
- Size: 914 KB
- Stars: 633
- Watchers: 13
- Forks: 115
- Open Issues: 31
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-chrome-devtools - pychrome - low level CDP transport handler (Alumni / Automation)
- fucking-awesome-chrome-devtools - pychrome - low level CDP transport handler (Alumni / Automation)
README
# pychrome
[](https://travis-ci.org/fate0/pychrome)
[](https://codecov.io/gh/fate0/pychrome)
[](https://pyup.io/repos/github/fate0/pychrome/)
[](https://pypi.python.org/pypi/pychrome)
[](https://github.com/fate0/pychrome)
A Python Package for the Google Chrome Dev Protocol, [more document](https://fate0.github.io/pychrome/)
## Table of Contents
* [Installation](#installation)
* [Setup Chrome](#setup-chrome)
* [Getting Started](#getting-started)
* [Tab management](#tab-management)
* [Debug](#debug)
* [Examples](#examples)
* [Ref](#ref)
## Installation
To install pychrome, simply:
```
$ pip install -U pychrome
```
or from GitHub:
```
$ pip install -U git+https://github.com/fate0/pychrome.git
```
or from source:
```
$ python setup.py install
```
## Setup Chrome
simply:
```
$ google-chrome --remote-debugging-port=9222
```
or headless mode (chrome version >= 59):
```
$ google-chrome --headless --disable-gpu --remote-debugging-port=9222
```
or use docker:
```
$ docker pull fate0/headless-chrome
$ docker run -it --rm --cap-add=SYS_ADMIN -p9222:9222 fate0/headless-chrome
```
## Getting Started
``` python
import pychrome
# create a browser instance
browser = pychrome.Browser(url="http://127.0.0.1:9222")
# create a tab
tab = browser.new_tab()
# register callback if you want
def request_will_be_sent(**kwargs):
print("loading: %s" % kwargs.get('request').get('url'))
tab.Network.requestWillBeSent = request_will_be_sent
# start the tab
tab.start()
# call method
tab.Network.enable()
# call method with timeout
tab.Page.navigate(url="https://github.com/fate0/pychrome", _timeout=5)
# wait for loading
tab.wait(5)
# stop the tab (stop handle events and stop recv message from chrome)
tab.stop()
# close tab
browser.close_tab(tab)
```
or (alternate syntax)
``` python
import pychrome
browser = pychrome.Browser(url="http://127.0.0.1:9222")
tab = browser.new_tab()
def request_will_be_sent(**kwargs):
print("loading: %s" % kwargs.get('request').get('url'))
tab.set_listener("Network.requestWillBeSent", request_will_be_sent)
tab.start()
tab.call_method("Network.enable")
tab.call_method("Page.navigate", url="https://github.com/fate0/pychrome", _timeout=5)
tab.wait(5)
tab.stop()
browser.close_tab(tab)
```
more methods or events could be found in
[Chrome DevTools Protocol](https://chromedevtools.github.io/devtools-protocol/tot/)
## Debug
set DEBUG env variable:

## Tab management
run `pychrome -h` for more info
example:

## Examples
please see the [examples](http://github.com/fate0/pychrome/blob/master/examples) directory for more examples
## Ref
* [chrome-remote-interface](https://github.com/cyrus-and/chrome-remote-interface/)
* [Chrome DevTools Protocol](https://chromedevtools.github.io/devtools-protocol/tot/)