{"id":4127,"library":"mycdp","title":"MyCDP: Autogenerated Chrome DevTools Protocol Client","description":"MyCDP is an autogenerated Python client for the Chrome DevTools Protocol (CDP), providing a direct, low-level interface to control Chrome and Chromium-based browsers. It wraps the latest CDP specification, making it highly synchronized with Chrome's internal API. Current version is 1.3.7, with updates released frequently to reflect changes in CDP.","status":"active","version":"1.3.7","language":"en","source_language":"en","source_url":"https://github.com/mdmintz/MyCDP","tags":["cdp","chrome","devtools","automation","browser","headless"],"install":[{"cmd":"pip install mycdp","lang":"bash","label":"Install stable version"}],"dependencies":[],"imports":[{"note":"The 'cdp' object provides access to all DevTools Protocol domains (e.g., cdp.page, cdp.network, cdp.browser).","symbol":"cdp","correct":"from mycdp import cdp"}],"quickstart":{"code":"import subprocess\nimport time\nimport os\nfrom mycdp import cdp\n\n# --- This quickstart requires a Chrome/Chromium browser executable in your PATH ---\n# Start Chrome in headless mode with remote debugging enabled on port 9222\n# Adjust 'google-chrome' to 'chromium', 'chrome', or the full path to your browser\nchrome_executable = os.environ.get('CHROME_EXECUTABLE', 'google-chrome')\nremote_debugging_port = os.environ.get('REMOTE_DEBUGGING_PORT', '9222')\n\nchrome_process = None\nws_url = f\"ws://localhost:{remote_debugging_port}\"\nsession = None\n\ntry:\n    print(f\"Launching {chrome_executable} with remote debugging on port {remote_debugging_port}...\")\n    # Note: stderr/stdout are redirected to PIPE to avoid polluting the console\n    chrome_process = subprocess.Popen(\n        [chrome_executable, '--headless=new', f'--remote-debugging-port={remote_debugging_port}'],\n        stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True\n    )\n    time.sleep(3) # Give Chrome a moment to start and open the debugging port\n\n    # Connect to the running Chrome instance\n    print(f\"Connecting to Chrome at {ws_url}...\")\n    session = cdp.browser.connect(ws_url)\n    print(\"Successfully connected to Chrome.\")\n\n    # Enable the Page domain to receive events and navigate\n    cdp.page.enable(session)\n\n    # Navigate to a URL\n    target_url = \"https://www.example.com\"\n    print(f\"Navigating to {target_url}...\")\n    cdp.page.navigate(session, url=target_url)\n    # Wait for the page to finish loading (load event)\n    cdp.page.wait_for_load_event(session)\n    print(f\"Successfully navigated to {target_url}\")\n\n    # Execute a JavaScript expression to get the page title\n    title_response = cdp.runtime.evaluate(session, expression=\"document.title\")\n    if title_response and title_response.result:\n        print(f\"Page title: {title_response.result.value}\")\n\n    # Example: Get all cookies\n    cookies_response = cdp.storage.get_cookies(session)\n    if cookies_response and cookies_response.cookies:\n        print(f\"Found {len(cookies_response.cookies)} cookies.\")\n        # for cookie in cookies_response.cookies:\n        #    print(f\" - {cookie.name}: {cookie.value}\")\n\nexcept Exception as e:\n    print(f\"An error occurred: {e}\")\n    # Print stderr if Chrome process is available and has output\n    if chrome_process:\n        try:\n            _, stderr_output = chrome_process.communicate(timeout=1)\n            if stderr_output:\n                print(\"Chrome stderr:\", stderr_output)\n        except subprocess.TimeoutExpired:\n            chrome_process.kill()\n            print(\"Chrome stderr: Process timed out during communicate, killed.\")\n\nfinally:\n    # Always close the CDP session and terminate the Chrome process\n    if session:\n        print(\"Closing CDP session...\")\n        cdp.browser.close(session)\n    if chrome_process and chrome_process.poll() is None:\n        print(\"Terminating Chrome process...\")\n        chrome_process.terminate()\n        try:\n            chrome_process.wait(timeout=5)\n        except subprocess.TimeoutExpired:\n            chrome_process.kill()\n            print(\"Chrome process did not terminate gracefully, killed.\")\n    print(\"Quickstart finished.\")","lang":"python","description":"This example launches a headless Chrome instance, connects to it via MyCDP, navigates to 'example.com', retrieves its title, and lists its cookies. It demonstrates connecting to a browser, enabling a CDP domain, and executing basic page and runtime commands. Ensure you have a Chrome/Chromium executable in your system's PATH (e.g., 'google-chrome', 'chromium')."},"warnings":[{"fix":"Update all references from `cdp.network.PrivateNetworkRequestPolicy` to `cdp.network.LocalNetworkAccessRequestPolicy`.","message":"The enum value `PrivateNetworkRequestPolicy` in the `Network` domain was renamed to `LocalNetworkAccessRequestPolicy` to align with upstream CDP changes.","severity":"breaking","affected_versions":">=1.3.7"},{"fix":"Regularly check Chrome DevTools Protocol release notes and MyCDP changelogs. Pin MyCDP versions and test thoroughly before upgrading when working with critical functionality.","message":"MyCDP is autogenerated directly from the Chrome DevTools Protocol specification. This means its API surface can change significantly with new Chrome releases, potentially introducing breaking changes or requiring updates to your code, even in minor MyCDP versions.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Ensure a browser is running with `--remote-debugging-port=<PORT>` (e.g., `--remote-debugging-port=9222`) before attempting to connect. Use `subprocess` or a similar method for automated browser launch, as demonstrated in the quickstart.","message":"MyCDP requires a running Chrome or Chromium browser instance with remote debugging enabled. It does not launch the browser itself by default. You must start the browser separately or manage its lifecycle within your application.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Wrap CDP operations in `try...finally` blocks to ensure `cdp.browser.close(session)` is called for every `cdp.browser.connect()`.","message":"Connections to the Chrome DevTools Protocol should always be explicitly closed using `cdp.browser.close(session)` when no longer needed to release resources and prevent potential memory leaks or hanging browser processes.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-11T00:00:00.000Z","next_check":"2026-07-10T00:00:00.000Z"}