Overview
HasCdp is a Java interface that enables executing raw Chrome DevTools Protocol (CDP) commands directly through a Chromium-based WebDriver instance.
Description
The HasCdp interface, annotated with @Beta, declares a single method executeCdpCommand(String commandName, Map<String, Object> parameters) that sends an arbitrary CDP command to the browser and returns the result as a name-value map. The command name and parameters should conform to the Chrome DevTools Protocol specification. The source explicitly notes that the higher-level org.openqa.selenium.devtools.DevTools API is strongly encouraged over this low-level interface.
Usage
Use HasCdp when you need to issue CDP commands that are not yet covered by the Selenium DevTools API, or when you need direct low-level access to specific protocol domains. Cast the driver to HasCdp to access the executeCdpCommand method.
Code Reference
Source Location
Signature
@Beta
public interface HasCdp {
Map<String, Object> executeCdpCommand(String commandName, Map<String, Object> parameters);
}
Import
import org.openqa.selenium.chromium.HasCdp;
I/O Contract
Methods
| Method |
Parameters |
Return Type |
Description
|
executeCdpCommand(String, Map) |
commandName -- the CDP command to execute (e.g., "Network.enable"); parameters -- a map of parameter names to values required by the command |
Map<String, Object> |
Executes the specified Chrome DevTools Protocol command and returns the result as a map of response field names to their values.
|
Input
| Parameter |
Type |
Description
|
commandName |
String |
A fully qualified CDP command name following the "Domain.method" convention (e.g., "Page.navigate", "Network.emulateNetworkConditions").
|
parameters |
Map<String, Object> |
A map containing the parameters expected by the CDP command. May be an empty map if the command takes no parameters.
|
Output
| Return Type |
Description
|
Map<String, Object> |
A map of response fields returned by the CDP command. The structure depends on the specific command invoked.
|
Usage Examples
ChromeDriver driver = new ChromeDriver();
// Cast the driver to HasCdp
HasCdp cdpDriver = (HasCdp) driver;
// Execute a CDP command to get the browser version
Map<String, Object> version = cdpDriver.executeCdpCommand(
"Browser.getVersion", Map.of());
System.out.println("Browser: " + version.get("product"));
System.out.println("User Agent: " + version.get("userAgent"));
// Navigate using CDP directly
cdpDriver.executeCdpCommand("Page.navigate", Map.of(
"url", "https://www.selenium.dev"
));
// Enable network tracking
cdpDriver.executeCdpCommand("Network.enable", Map.of());
// Set a cookie via CDP
cdpDriver.executeCdpCommand("Network.setCookie", Map.of(
"name", "testCookie",
"value", "testValue",
"domain", "www.selenium.dev"
));
// Note: Prefer using org.openqa.selenium.devtools.DevTools API
// for type-safe, versioned CDP interaction when available.
driver.quit();
Related Pages
Page Connections
Double-click a node to navigate. Hold to expand connections.