Overview
TheChrome class is the main driver class that controls ChromeDriver and allows you to drive the browser with built-in detection avoidance. It extends Selenium’s WebDriver class and automatically handles ChromeDriver download, patching, and configuration.
Constructor
Chrome()
Creates a new instance of the Chrome driver with automatic detection avoidance.Parameters
ChromeOptions instance to customize browser behavior. When
None, automatic useful defaults are applied. Using custom options may lower undetectability.Path to a Chrome profile directory. If specified, uses that profile and disables automatic removal at exit. If
None, creates a temporary profile.Path to the ChromeDriver executable. When
None, automatically downloads and patches a new binary.Path to the Chrome/Chromium browser executable. If not specified, uses the system’s default Chrome installation.
Port used by the ChromeDriver executable (NOT the debugger port). Value of
0 automatically picks an available port. Leave at default unless you know what you’re doing.Enables Chrome DevTools Protocol event handling. When enabled, you can subscribe to CDP events using
add_cdp_listener().Dictionary with non-browser specific capabilities like “proxy” or “loggingPrefs”. Auto-generated from options when
None.Makes WebElement repr more readable, showing HTML-like representation instead of session/element IDs. Useful for interactive environments.Example:
- Default:
<selenium.webdriver.remote.webelement.WebElement (session="...", element="...")> - Advanced:
<WebElement(<a class="nav-link" href="#" id="menu">)>
Whether to configure ChromeRemoteConnection to use HTTP keep-alive.
Chrome log level. Default adapts to Python’s global log level.
Run browser in headless mode. Can also be specified in the options instance. Warning: This lowers undetectability and is not fully supported.
Suppresses the “set as default browser” welcome alert on Unix-like systems. Set to
False only if you want to handle the alert manually.When
False, Chrome gets its own process (not a subprocess of ChromeDriver). This fixes multithreading issues and ensures proper shutdown. Set to True only for simple single-script usage.Note: Setting to True comes with NO support when being detected.Enable debug mode which logs all method calls with their arguments.
Uses the
--no-sandbox Chrome option and suppresses the “unsecure option” status bar. Required when running as root.Set to
True when using multithreading/multiprocessing. Prevents race conditions when multiple processes try to patch the same binary.Important: When using this option, you MUST call Patcher.patch() before creating Chrome instances:Example Usage
Methods
get()
Navigates to a URL.The URL to navigate to.
add_cdp_listener()
Subscribes to Chrome DevTools Protocol events. Only works whenenable_cdp_events=True.
The CDP event name (e.g., “Network.responseReceived”, “Network.requestWillBeSent”).
Function that accepts exactly one parameter (the event message dict).
False if reactor is not enabled.
clear_cdp_listeners()
Removes all CDP event listeners.window_new()
Opens a new browser window.tab_new()
Opens a URL in a new tab using CDP.The URL to open in the new tab.
reconnect()
Reconnects to Chrome by stopping and restarting the ChromeDriver service. Useful for heavy detection avoidance.Time to wait between stopping and starting the service (in seconds).
start_session()
Starts a new WebDriver session. Unlike the standard Selenium method, capabilities are automatically recreated from options.Browser capabilities. Auto-generated from options if not provided.
Browser profile (for compatibility, not typically used).
find_elements_recursive()
Finds elements across all frames (including iframes). Returns a generator that yields elements as it searches.The locator strategy (e.g., By.ID, By.CSS_SELECTOR).
The locator value.
quit()
Closes the browser and cleans up resources including temporary profile.Attributes
The current WebDriver session ID.
Whether debug mode is enabled.
The CDP event reactor instance (only available when
enable_cdp_events=True).The Patcher instance used to download and patch ChromeDriver.
The ChromeOptions instance used to configure the browser.
Path to the user data directory (Chrome profile).
Whether the user data directory should be preserved after quit.
The process ID of the Chrome browser.
Context Manager Support
The Chrome class supports Python’s context manager protocol:Important Notes
The ChromeDriver binary is automatically downloaded and patched. You don’t need to manage it manually unless using
user_multi_procs=True.For best undetectability, avoid:
- Custom extensions
- Non-default startup options
- Headless mode
- Running as subprocess (
use_subprocess=True)