Implementation:SeleniumHQ Selenium ChromeDriver Constructor For CDP
| Knowledge Sources | |
|---|---|
| Domains | Browser_Automation, DevTools, Session_Management |
| Last Updated | 2026-02-11 00:00 GMT |
Overview
Concrete tool for creating a Chrome WebDriver session with DevTools Protocol access provided by the Selenium Java library.
Description
The ChromeDriver class extends ChromiumDriver, which implements HasDevTools (and HasBiDi, HasAuthentication, HasLogEvents). This means every Chrome session automatically includes CDP capabilities. The key is the se:cdp capability in the session response, which provides the WebSocket URL for the DevTools connection. The HasDevTools interface provides getDevTools() (throws DevToolsException on failure) and maybeGetDevTools() (returns Optional<DevTools>). The ChromeDriver constructor also initializes casting and CDP command support via AddHasCasting and AddHasCdp helper classes.
Usage
Use this when your goal is to access CDP features (network interception, console capture, performance monitoring). Create a standard ChromeDriver, then call getDevTools() directly since ChromeDriver inherits the method from ChromiumDriver.
Code Reference
Source Location
- Repository: Selenium
- File:
java/src/org/openqa/selenium/chrome/ChromeDriver.java(L42-132) - File:
java/src/org/openqa/selenium/devtools/HasDevTools.java(L22-30) - File:
java/src/org/openqa/selenium/chromium/ChromiumDriver.java(implements HasDevTools)
Signature
// ChromeDriver extends ChromiumDriver which implements HasDevTools
public class ChromeDriver extends ChromiumDriver {
public ChromeDriver();
public ChromeDriver(ChromeDriverService service);
public ChromeDriver(ChromeOptions options);
public ChromeDriver(ChromeOptions options, ClientConfig clientConfig);
public ChromeDriver(ChromeDriverService service, ChromeOptions options);
public ChromeDriver(ChromeDriverService service, ChromeOptions options, ClientConfig clientConfig);
}
// HasDevTools interface
public interface HasDevTools {
default DevTools getDevTools();
Optional<DevTools> maybeGetDevTools();
}
Import
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.devtools.HasDevTools;
import org.openqa.selenium.devtools.DevTools;
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| options | ChromeOptions | No | Chrome configuration (defaults to new ChromeOptions() if omitted) |
| service | ChromeDriverService | No | Driver service (defaults to ChromeDriverService.createDefaultService() if omitted) |
| clientConfig | ClientConfig | No | HTTP client configuration (defaults to ClientConfig.defaultConfig() if omitted) |
Outputs
| Name | Type | Description |
|---|---|---|
| driver | ChromeDriver | Active session implementing HasDevTools with "se:cdp" capability |
| getDevTools() | DevTools | CDP client; throws DevToolsException if connection fails |
| maybeGetDevTools() | Optional<DevTools> | CDP client wrapped in Optional; empty if connection fails |
Usage Examples
Create CDP-Enabled Session
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.devtools.DevTools;
ChromeDriver driver = new ChromeDriver();
DevTools devTools = driver.getDevTools();
devTools.createSession();
// Now use devTools for CDP operations
// ...
devTools.close();
driver.quit();
Create Session with Custom Options
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.devtools.DevTools;
ChromeOptions options = new ChromeOptions();
options.addArguments("--headless=new");
ChromeDriver driver = new ChromeDriver(options);
// getDevTools() inherited from ChromiumDriver via HasDevTools
DevTools devTools = driver.getDevTools();
devTools.createSession();
// CDP operations available even in headless mode
// ...
devTools.close();
driver.quit();