Overview
ThePatcher class manages ChromeDriver binary downloads, version matching, and applies patches to avoid detection. It automatically downloads the correct ChromeDriver version for your Chrome browser and patches it to remove detection vectors.
Constructor
Patcher()
Creates a new Patcher instance.The browser’s main/major version number (e.g., 120, 121).
Full file path to the ChromeDriver executable. When
None, automatically determines the path.Set to
True when using multithreading/multiprocessing to prevent race conditions.Internal flag indicating the class is only being used to call the
patch() method.Class Methods
patch()
Static method to download and patch ChromeDriver. Must be called before using Chrome in multiprocessing contexts.Path to the Chrome browser executable. Used to detect the browser version.
Path where the patched ChromeDriver should be saved.
cleanup_unused_files()
Removes old ChromeDriver binaries from the data directory.kill_all_instances()
Kills all running instances of a ChromeDriver executable.Path to the ChromeDriver executable.
Instance Methods
verify()
Verifies that a patched ChromeDriver binary exists and is valid.True if a valid patched binary is found, False otherwise.
download_and_patch()
Downloads the appropriate ChromeDriver version and patches it.True if the binary was successfully patched.
fetch_release_number()
Fetches the latest full version number for the specified major version.Version object with the full version number.
fetch_package()
Downloads the ChromeDriver package from Google’s servers.unzip_package()
Extracts ChromeDriver from the downloaded package.Path to the ZIP file to extract.
patch_exe()
Patches the ChromeDriver binary to remove detection vectors.is_binary_patched()
Checks if a ChromeDriver binary is already patched.Path to check. Uses instance’s
driver_executable_path if not specified.True if the binary is patched, False otherwise.
driver_binary_in_use()
Checks if a ChromeDriver binary is currently running.Path to the binary to check. Uses instance’s path if not specified.
True if the binary is in use, False otherwise.
parse_exe_version()
Extracts the version number from a ChromeDriver binary.Version object parsed from the binary.
Attributes
Platform-specific directory where ChromeDriver binaries are stored:
- Windows:
~/appdata/roaming/undetected - Linux:
~/.local/share/undetected - macOS:
~/Library/Application Support/undetected - Lambda:
/tmp/undetected
Full path to the ChromeDriver executable.
Browser major version number.
Full ChromeDriver version including minor, build, and patch numbers.
True if the version is 114 or older (uses legacy download URLs).Base URL for downloading ChromeDriver:
- Chrome ≤114:
https://chromedriver.storage.googleapis.com - Chrome ≥115:
https://googlechromelabs.github.io/chrome-for-testing
Platform identifier for downloads:
win32, linux64, mac-x64, or mac64.ChromeDriver executable name:
chromedriver.exe (Windows) or chromedriver (Unix).How Patching Works
The patcher performs the following steps:- Version Detection: Detects the Chrome browser version installed on your system
- Download: Downloads the matching ChromeDriver version from Google’s CDN
- Extract: Unzips the ChromeDriver package
- Patch: Replaces detection code blocks with benign code:
- Finds:
{window.cdc...;} - Replaces with:
{console.log("undetected chromedriver 1337!")}
- Finds:
- Verify: Confirms the binary is properly patched
Multiprocessing Support
When using multiprocessing or multithreading, you must callPatcher.patch() once before creating Chrome instances:
Version Compatibility
The Patcher handles two different ChromeDriver distribution systems:Chrome 114 and Earlier
- URL:
https://chromedriver.storage.googleapis.com - Format:
https://chromedriver.storage.googleapis.com/{version}/chromedriver_{platform}.zip
Chrome 115 and Later
- URL:
https://googlechromelabs.github.io/chrome-for-testing - Format: Uses JSON manifest with download URLs
- ZIP structure includes platform-specific subdirectories
Important Notes
The Patcher automatically cleans up old ChromeDriver binaries when a new version is downloaded. Only the most recent binary is kept.
ChromeDriver binaries are stored in platform-specific directories and automatically managed. You typically don’t need to specify custom paths unless you have specific requirements.
The patcher uses file locking to prevent race conditions when multiple processes try to patch simultaneously. This is handled automatically.