Trio Chrome DevTools Protocol

0.6.0 · active · verified Thu Apr 16

Trio Chrome DevTools Protocol (Trio CDP) is a Python library that enables remote control of any web browser implementing the Chrome DevTools Protocol. It leverages Trio for asynchronous I/O and `python-chrome-devtools-protocol` for type wrappers. The library handles WebSocket negotiation and session management, allowing transparent multiplexing of commands, responses, and events over a single connection. The current version is 0.6.0, released in April 2020, with ongoing maintenance evident through recent GitHub activity.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart connects to a headless Chrome instance (assumed to be running on `http://localhost:9222`), navigates to a specified URL, waits for the page to load, and then extracts the HTML content of the page's title element. It demonstrates basic connection, session management, navigation, and DOM interaction using Trio's async capabilities. Before running, ensure Chrome is launched with `--remote-debugging-port=9222`.

import trio
from trio_cdp import open_cdp, page, dom

async def main():
    # Ensure Chrome is running with --remote-debugging-port=9222
    cdp_url = 'http://localhost:9222'
    target_url = 'https://www.example.com'

    try:
        async with open_cdp(cdp_url) as conn:
            # Find the first available target (usually a browser tab).
            # Note: target domain imports are typically implicitly available on 'conn.target'
            targets = await conn.target.get_targets()
            if not targets:
                print('No targets found. Is Chrome running and a page open?')
                return
            target_id = targets[0].id

            # Create a new session with the chosen target.
            async with conn.open_session(target_id) as session:
                # Enable page-level events to wait for navigation completion.
                async with session.page_enable():
                    async with session.wait_for(page.LoadEventFired):
                        await session.execute(page.navigate(target_url))

                print(f"Navigated to: {target_url}")

                # Extract the page title.
                root_node = await session.execute(dom.get_document())
                title_node_id = await session.execute(dom.query_selector(root_node.node_id, 'title'))
                if title_node_id:
                    html = await session.execute(dom.get_outer_html(title_node_id))
                    print(f"Page Title HTML: {html}")
                else:
                    print("Could not find title element.")

    except Exception as e:
        print(f"An error occurred: {e}")
        print("Make sure Chrome is running with '--remote-debugging-port=9222'.")

if __name__ == '__main__':
    trio.run(main)

view raw JSON →