PyChromeDevTools

repository·master·Indexed 43 days ago

https://github.com/marty90/pychromedevtools

A Python wrapper for the Chrome DevTools Protocol that enables programmatic interaction with Google Chrome for automation, benchmarking, and network inspection. It provides the ChromeInterface class to connect to the browser, send commands to protocol domains, and handle browser events using methods like wait_message, wait_event, and pop_messages.

Tokens
1.6K
Snippets
7
Records
8
Agent score
6%

What's inside PyChromeDevTools

  1. Configure Google Chrome for remote debugging

    master

    To use PyChromeDevTools, you must launch Google Chrome with the remote-debugging-port option enabled. This allows the Python module to communicate with the browser via the Chrome DevTools Protocol.

    Run the following command to start Chrome with debugging enabled on port 9222:

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

    Additional Options:

    • --enable-benchmarking and --enable-net-benchmarking: Enable advanced Chrome benchmarking capabilities.
    • --headless: Run Chrome without a visible user interface.
    google-chrome --remote-debugging-port=9222 --remote-allow-origins=*
  2. Install PyChromeDevTools

    master

    Install PyChromeDevTools and its required dependencies (requests and websocket-client) using pip:

    sudo pip3 install PyChromeDevTools
    sudo pip3 install PyChromeDevTools
  3. Example: Print all object URLs of a page

    master

    This example iterates through the messages received during a page load to find Network.responseReceived events and prints the URL of every resource loaded.

    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
  4. Example: Measure Page Loading Time

    master

    This example enables the Network and Page domains, navigates to a URL, and waits for the Page.loadEventFired event to calculate the total 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)
  5. Example: Print all installed cookies

    master

    This example enables the Network and Page domains, navigates to a site, waits for the frame to stop loading, and then retrieves and prints all 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")
  6. Run Chrome DevTools commands

    master

    To send a command to Chrome, invoke the corresponding method on the ChromeInterface object. The method names correspond to the Chrome DevTools Protocol domains (e.g., Page.navigate, Network.getCookies).

    Commands return a tuple containing:

    1. The command's return value (already interpreted as JSON).
    2. A list of all messages/events received before the command was run.
    return_value, messages = chrome.Page.navigate(url="http://example.com/")
  7. Initialize ChromeInterface

    master

    Create a ChromeInterface object to establish a connection to the running Chrome instance. By default, it connects to localhost:9222.

    Custom Host and Port: You can specify a different host and port manually:

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

    Android Support: When working in Android environments, set suppress_origin=True to suppress the Origin Header in the created websockets:

    chrome = PyChromeDevTools.ChromeInterface(suppress_origin=True)

    Direct Target Connection: To bypass querying the JSON for a tab ID and connect directly to a specific target, use connect_targetID:

    chrome.connect_targetID(targetID="YOUR_TARGET_ID")
    chrome = PyChromeDevTools.ChromeInterface()
  8. Receive and handle browser events

    master

    Chrome sends messages for specific browser events. All unread events are cleared before any new command is executed. You can retrieve events in three ways (all returned as interpreted JSON):

    1. Pop a single message

    Use wait_message() to retrieve one message from the queue. It is blocking and accepts an optional timeout in seconds (default is 1).

    message = chrome.wait_message(timeout=5)

    2. Wait for a specific event

    Use wait_event(event_name) to wait until a specific event arrives. It returns a tuple: (matching_event, all_events), where matching_event is the first event matching the name, and all_events contains all events that arrived before it.

    matching_event, all_events = chrome.wait_event("Page.loadEventFired", timeout=60)

    3. Get all received messages

    Use pop_messages() to retrieve all currently queued messages. This method is non-blocking and does not use a timeout.

    messages = chrome.pop_messages()