Selenium Chrome Driver provides WebDriver implementation for Google Chrome with Chrome DevTools Protocol support, network conditions simulation, permissions management, media casting capabilities, and Chrome-specific service configuration.
—
The Chrome DevTools Protocol (CDP) integration allows direct communication with Chrome's debugging interface for advanced browser control, performance monitoring, and debugging capabilities.
Execute Chrome DevTools Protocol commands directly.
public Map<String, Object> executeCdpCommand(String commandName, Map<String, Object> parameters);Parameters:
commandName (String): CDP command name following the format "Domain.method"parameters (Map<String, Object>): Command parameters as key-value pairsReturns: Map<String, Object> - Command response data
Usage Example:
import java.util.Map;
import java.util.HashMap;
ChromiumDriver driver = new ChromiumDriver(commandExecutor, capabilities, "chrome");
// Take a screenshot using CDP
Map<String, Object> result = driver.executeCdpCommand("Page.captureScreenshot", Map.of(
"format", "png",
"quality", 100
));
String screenshotData = (String) result.get("data");
// Get performance metrics
Map<String, Object> metrics = driver.executeCdpCommand("Performance.getMetrics", Map.of());
// Enable network domain and capture requests
driver.executeCdpCommand("Network.enable", Map.of());
driver.executeCdpCommand("Network.setCacheDisabled", Map.of("cacheDisabled", true));Control page-level operations:
// Navigate to URL
driver.executeCdpCommand("Page.navigate", Map.of("url", "https://example.com"));
// Reload page
driver.executeCdpCommand("Page.reload", Map.of("ignoreCache", true));
// Get page layout metrics
Map<String, Object> layout = driver.executeCdpCommand("Page.getLayoutMetrics", Map.of());
// Add script to evaluate on new document
driver.executeCdpCommand("Page.addScriptToEvaluateOnNewDocument", Map.of(
"source", "console.log('Page loaded');"
));JavaScript execution and console interaction:
// Evaluate JavaScript expression
Map<String, Object> result = driver.executeCdpCommand("Runtime.evaluate", Map.of(
"expression", "document.title",
"returnByValue", true
));
// Enable runtime notifications
driver.executeCdpCommand("Runtime.enable", Map.of());
// Call function on object
driver.executeCdpCommand("Runtime.callFunctionOn", Map.of(
"functionDeclaration", "function() { return this.tagName; }",
"objectId", "node-id"
));Monitor and control network activity:
// Enable network tracking
driver.executeCdpCommand("Network.enable", Map.of());
// Set user agent override
driver.executeCdpCommand("Network.setUserAgentOverride", Map.of(
"userAgent", "Custom User Agent 1.0"
));
// Block URLs matching patterns
driver.executeCdpCommand("Network.setBlockedURLs", Map.of(
"urls", Arrays.asList("*analytics*", "*tracking*")
));
// Clear browser cache
driver.executeCdpCommand("Network.clearBrowserCache", Map.of());Performance monitoring and metrics:
// Enable performance metrics collection
driver.executeCdpCommand("Performance.enable", Map.of());
// Get current metrics
Map<String, Object> metrics = driver.executeCdpCommand("Performance.getMetrics", Map.of());
// Disable performance monitoring
driver.executeCdpCommand("Performance.disable", Map.of());Security-related operations:
// Enable security notifications
driver.executeCdpCommand("Security.enable", Map.of());
// Set ignore certificate errors
driver.executeCdpCommand("Security.setIgnoreCertificateErrors", Map.of(
"ignore", true
));DOM manipulation and inspection:
// Get document root
Map<String, Object> doc = driver.executeCdpCommand("DOM.getDocument", Map.of());
// Query selector
Map<String, Object> element = driver.executeCdpCommand("DOM.querySelector", Map.of(
"nodeId", 1,
"selector", "#myElement"
));
// Get element attributes
Map<String, Object> attrs = driver.executeCdpCommand("DOM.getAttributes", Map.of(
"nodeId", 123
));Get the DevTools instance for advanced operations:
public Optional<DevTools> maybeGetDevTools();Returns: Optional<DevTools> - DevTools instance if available
Usage Example:
import org.openqa.selenium.devtools.DevTools;
Optional<DevTools> devToolsOpt = driver.maybeGetDevTools();
if (devToolsOpt.isPresent()) {
DevTools devTools = devToolsOpt.get();
devTools.createSession();
// Use DevTools API instead of raw CDP
devTools.send(org.openqa.selenium.devtools.v85.network.Network.enable(
Optional.empty(), Optional.empty(), Optional.empty()
));
}Get WebDriver BiDi instance for modern protocol support:
public Optional<BiDi> maybeGetBiDi();Returns: Optional<BiDi> - BiDi instance if available
Usage Example:
import org.openqa.selenium.bidi.BiDi;
Optional<BiDi> biDiOpt = driver.maybeGetBiDi();
if (biDiOpt.isPresent()) {
BiDi biDi = biDiOpt.get();
// Use BiDi protocol features
}// Enable network domain
driver.executeCdpCommand("Network.enable", Map.of());
// Set request interception
driver.executeCdpCommand("Network.setRequestInterception", Map.of(
"patterns", Arrays.asList(Map.of(
"urlPattern", "*",
"resourceType", "Document",
"interceptionStage", "HeadersReceived"
))
));
// Continue intercepted request
driver.executeCdpCommand("Network.continueInterceptedRequest", Map.of(
"interceptionId", "interceptionId",
"rawResponse", "modifiedResponseData"
));// Get all cookies
Map<String, Object> cookies = driver.executeCdpCommand("Network.getAllCookies", Map.of());
// Set cookie
driver.executeCdpCommand("Network.setCookie", Map.of(
"name", "test_cookie",
"value", "test_value",
"domain", "example.com",
"path", "/",
"secure", true,
"httpOnly", false
));
// Delete cookies
driver.executeCdpCommand("Network.deleteCookies", Map.of(
"name", "test_cookie",
"domain", "example.com"
));// Enable runtime to receive console messages
driver.executeCdpCommand("Runtime.enable", Map.of());
// Console messages will be available through CDP events
// Note: Event handling requires DevTools API or custom event listeners// Command constant for CDP execution
public static final String EXECUTE_CDP = "executeCdpCommand";
// DevTools connection and session management
public class DevTools {
public void createSession();
public void createSessionIfThereIsNotOne();
public <T> T send(Command<T> command);
}
// BiDi protocol support
public class BiDi {
// BiDi protocol methods
}Common exceptions when using CDP:
Usage Example with Error Handling:
try {
Map<String, Object> result = driver.executeCdpCommand("Page.captureScreenshot", Map.of(
"format", "png"
));
String screenshot = (String) result.get("data");
} catch (WebDriverException e) {
System.err.println("CDP command failed: " + e.getMessage());
}For complete CDP command documentation, refer to:
Install with Tessl CLI
npx tessl i tessl/maven-org-seleniumhq-selenium--selenium-chromium-driver