A Python Package for the Google Chrome Dev Protocol

0.2.4 · active · verified Thu Apr 16

pychrome is a Python package designed for interacting with Google Chrome via the Chrome DevTools Protocol. It enables automation and inspection of web pages, allowing users to control browser behavior, capture network traffic, manipulate the DOM, and more. The current version is 0.2.4, with development actively maintained on GitHub.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to connect to a running Chrome instance with remote debugging enabled, create a new tab, navigate to a URL, register a callback for network requests, and execute JavaScript to get the page title. It includes a robust way to ensure Chrome is running on the correct port before attempting to connect.

import pychrome
import os
import subprocess
import time

# --- Step 1: Ensure Chrome is running with remote debugging enabled ---
# This command typically opens Chrome (or a headless instance) on port 9222.
# Adjust the path to your Chrome executable if needed.
# For headless mode: 'google-chrome --headless --disable-gpu --remote-debugging-port=9222'

# Check if Chrome is already running on the debug port
try:
    # Attempt to connect to check if a browser is already listening
    _ = pychrome.Browser(url="http://127.0.0.1:9222").list_tab()
    print("Chrome with remote debugging already running.")
except Exception:
    print("Starting Chrome with remote debugging...")
    # Example for Linux/macOS. Adjust for Windows (e.g., 'start chrome.exe ...')
    # Using os.environ.get for robustness in different environments
    chrome_cmd = os.environ.get('CHROME_EXECUTABLE', 'google-chrome')
    try:
        # Start Chrome in headless mode for automation
        subprocess.Popen([chrome_cmd, '--headless', '--disable-gpu', '--remote-debugging-port=9222'],
                           stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
        time.sleep(2) # Give Chrome a moment to start up
    except FileNotFoundError:
        print(f"Error: Chrome executable '{chrome_cmd}' not found. Please set CHROME_EXECUTABLE environment variable or ensure Chrome is in your PATH.")
        exit(1)

# --- Step 2: Connect to Chrome and perform actions ---
browser = pychrome.Browser(url="http://127.0.0.1:9222")

# List existing tabs or create a new one
tabs = browser.list_tab()
if not tabs:
    tab = browser.new_tab()
else:
    # Use the first available tab, or iterate to find a specific one
    tab = tabs[0]

def request_will_be_sent(**kwargs):
    """Callback function to print URLs of outgoing requests"""
    url = kwargs.get('request', {}).get('url')
    if url:
        print(f"  Loading: {url}")

try:
    print(f"Interacting with tab: {tab.id}")
    tab.start()
    tab.Network.enable()
    tab.Network.requestWillBeSent = request_will_be_sent # Register callback

    print("Navigating to example.com...")
    tab.Page.navigate(url="https://www.example.com", _timeout=5)
    tab.wait(5) # Wait for page to load events for 5 seconds

    # Execute some JavaScript on the page
    result = tab.Runtime.evaluate(expression="document.title")
    print(f"Page title: {result['result']['value']}")

finally:
    # Clean up: stop the tab and close it
    if tab:
        tab.stop()
        browser.close_tab(tab.id)
        print(f"Closed tab: {tab.id}")
    # In a real application, you might also want to close the browser process
    # if it was started by your script, but care is needed not to close 
    # a user's active browser session.

view raw JSON →