CDP-Patches

1.1 · deprecated · verified Thu Apr 16

CDP-Patches is a Python library designed for patching Chrome DevTools Protocol (CDP) leaks at the operating system level. It aimed to address issues like input domain leaks and the inability of CDP to dispatch coalesced events, making web automation with tools like Playwright and Selenium more robust against bot detection. Version 1.1, released on September 28, 2025, represents its latest and likely final iteration.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use `cdp-patches` with Selenium to perform an OS-level click on a web element. It highlights the use of `SyncInput` to interact with a headful Chrome browser instance. This approach bypasses standard CDP input commands, which might be detected as automation, by dispatching events at the operating system level.

import os
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.remote.webelement import WebElement
from cdp_patches.input import SyncInput

# NOTE: This example requires a ChromeDriver executable in your PATH
# and a headful Chrome browser to run, as cdp-patches works on OS-level events.

def get_locator_pos(locator: WebElement):
    location = locator.location
    size = locator.size
    assert location and size
    x, y, width, height = location.get("x"), location.get("y"), size.get("width"), size.get("height")
    assert x is not None and y is not None and width is not None and height is not None
    # Calculate center of the element
    x, y = x + width // 2, y + height // 2
    return x, y

options = webdriver.ChromeOptions()
# Disable logs & automation flags for stealth (recommended for general automation, not specific to cdp-patches)
options.add_experimental_option("excludeSwitches", ["enable-logging", "enable-automation"])
options.add_experimental_option("useAutomationExtension", False)
options.add_argument("--log-level=3")

# Ensure Chrome is launched headfully
# For local testing, ensure ChromeDriver is in PATH or specify service.executable_path

try:
    with webdriver.Chrome(options=options) as driver:
        # cdp-patches operates on the browser's process ID (PID) or WebDriver instance
        sync_input = SyncInput(browser=driver)

        driver.get("https://www.google.com")

        # Example: Find a search bar and click it using OS-level input
        search_bar = driver.find_element(By.NAME, "q")
        x, y = get_locator_pos(search_bar)
        print(f"Clicking search bar at: ({x}, {y})")
        sync_input.click("left", x, y)
        print("Successfully clicked element using CDP-Patches SyncInput.")

except Exception as e:
    print(f"An error occurred: {e}")
    print("Please ensure Chrome browser and ChromeDriver are installed and configured correctly.")
    print("Also, remember cdp-patches requires a headful browser.")

view raw JSON →