• Stars
    star
    299
  • Rank 139,269 (Top 3 %)
  • Language
    Python
  • License
    Apache License 2.0
  • Created over 7 years ago
  • Updated 9 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

PyChromeDevTools is a python module that allows one to interact with Google Chrome using Chrome DevTools Protocol within a Python script.

PyChromeDevTools

1. Description

PyChromeDevTools is a python module that allows one to interact with Google Chrome using Chrome DevTools Protocol within a Python script. To use this tool, you must run an instance of Google Chrome with the remote-debugging option, like in the following example.

google-chrome --remote-debugging-port=9222 --remote-allow-origins=*

You may want to enable further Chrome benchmarking capabilities using the --enable-benchmarking and --enable-net-benchmarking options. You can run Chrome in headless mode using the option --headless.

For information about this Readme file and this tool please write to [email protected]

2. Prerequisites and Installation

Very few dependencies must be satisfied: an updated Google-Chrome version and the python packages requests and websocket. You can install them using the pip tool.

You can install PyChromeDevTools issuing the git command:

git clone https://github.com/marty90/PyChromeDevTools

Or, better, you install it and its dependencies by using pip:

sudo pip3 install PyChromeDevTools

3. Operation

3.1 Init

In your python script, as first, you must create a ChromeInterface object, like in the following:

chrome = PyChromeDevTools.ChromeInterface()

You can specify the host and the port of Chrome manually writing:

chrome = PyChromeDevTools.ChromeInterface(host="1.1.1.1",port=1234)

By default it uses localhost:9222.

You can connect directly to a targetID using the method connect_targetID. You must pass the targetID as parameter to this function. In this case, you avoid querying the json and finding the tab id.

3.1 Run commands

To send a command to Chrome, just invoke the corresponding method on the ChromeInterface object, and pass the desired parameters. For example, to visit a page write:

return_value, messages = chrome.Page.navigate(url="http://example.com/")

The return value of the command is passed as return value of the function, already interpreted as JSON. In messages, there are all the messages received before.

3.1 Receive Events

Chrome sends back messages for particular events in the browser. You can get them in three ways; they are returned already interpreted as JSON. All unread events are erased before any new command is run.

a) You can pop one message from the queue of received ones writing:

message=chrome.wait_message()

The method accepts an optional parameter timeout which is the value in seconds after which it gives up and returns None. Default is 1.

b) You can wait for a specific event writing:

matching_event,all_events=chrome.wait_event(event_name)

It waits until an event with the name event_name arrives, or a timeout elapses. matching_event contains the first found event that has event_name, while all_events contains all events arrived before. Timeout value can be configured as in the previous method.

c) You can get all already received messages writing:

messages=chrome.pop_messages()

This method is not blocking, and, thus, no timeout is used.

4. Examples

4.1 Page Loading Time

import PyChromeDevTools
import time

chrome = PyChromeDevTools.ChromeInterface()
chrome.Network.enable()
chrome.Page.enable()

start_time=time.time()
chrome.Page.navigate(url="http://www.google.com/")
chrome.wait_event("Page.loadEventFired", timeout=60)
end_time=time.time()

print ("Page Loading Time:", end_time-start_time)

4.2 Print all installed cookies

import PyChromeDevTools
import time

chrome = PyChromeDevTools.ChromeInterface()
chrome.Network.enable()
chrome.Page.enable()

chrome.Page.navigate(url="http://www.nytimes.com/")
chrome.wait_event("Page.frameStoppedLoading", timeout=60)

#Wait last objects to load
time.sleep(5)

cookies,messages = chrome.Network.getCookies()
for cookie in cookies["result"]["cookies"]:
    print ("Cookie:")
    print ("\tDomain:", cookie["domain"])
    print ("\tKey:", cookie["name"])
    print ("\tValue:", cookie["value"])
    print ("\n")

4.3 Print all object URLs of a page

import PyChromeDevTools

chrome = PyChromeDevTools.ChromeInterface()
chrome.Network.enable()
chrome.Page.enable()

chrome.Page.navigate(url="http://www.facebook.com")
event,messages=chrome.wait_event("Page.frameStoppedLoading", timeout=60)

for m in messages:
    if "method" in m and m["method"] == "Network.responseReceived":
        try:
            url=m["params"]["response"]["url"]
            print (url)
        except:
            pass

More Repositories

1

DPDK-Replay

This tool can replay a pcap capture at wire speed on several 10Gbps links.
C
67
star
2

multidocker

Creates a system where users are forced to login in dedicated independent docker containers.
Dockerfile
53
star
3

DPDK-Dump

DPDK-Dump is able to store on disk network traffic at high speed using DPDK library.
C
32
star
4

fastplot

Are you bored of re-writing code for each plot? Create publication-quality plots with a simple interface over matplotlib.
Python
24
star
5

WebTrafficGenerator

This tool creates web traffic by means of Selenium Web Browser
Python
15
star
6

errant

EmulatoR of Radio Access NeTworks
Jupyter Notebook
14
star
7

Network-Conditions-Emulator

Artificially limit uplink and downlink bandwidth, delay and loss rate on selected interfaces
Shell
13
star
8

netlytics

NetLytics is a Hadoop-powered framework for performing advanced analytics on various kinds of networks logs
Python
9
star
9

URL-generator

Use a GAN to train a generator and a discriminator over a set of URLs.
Python
9
star
10

priv-accept

Jupyter Notebook
6
star
11

spark-distance-matrix

Use Apache Spark to compute a distance matrix for a set of strings
Python
6
star
12

fast-exam

Create randomized exams in PDF, based on a data base of exercises and using Latex.
Python
4
star
13

icloud-private-relay-experiments

Shell
3
star
14

Spark-Command-Line-Tool

Spark Command Line Tool allows one to set up a Spark job using a command line. All functions must be Python expressions.
Python
2
star
15

Network-Monitoring-Python-APIs

I provide a module with few function frequently used while processing network monitoring data such as log files.
Python
2
star
16

tstat_spark_miner

This tool allows to perform simple queries to a Spark based cluster where Tstat log files are stored.
Python
2
star
17

webrtc-tester

Simple WebRTC testing automation
JavaScript
2
star
18

rtc_pcap_cleaners

Clean a capture file to make Zoom RTP flows look like standard ones.
Python
2
star
19

DPMon

Differentially-private query engine for Passive Network Measurements
Python
2
star
20

pyspeedtest

Simple package that uses Selenium to automate tests from speedtest.net
Jupyter Notebook
1
star
21

mobile-latency-emulator

Emulate mobile network latency based on large scale real-world measurements
Shell
1
star