CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-org-seleniumhq-selenium--selenium-chromium-driver

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.

Pending
Overview
Eval results
Files

devtools-protocol.mddocs/

DevTools Protocol

The Chrome DevTools Protocol (CDP) integration allows direct communication with Chrome's debugging interface for advanced browser control, performance monitoring, and debugging capabilities.

CDP Command Execution

Execute CDP Command

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 pairs

Returns: 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));

Common CDP Commands

Page Domain

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');"
));

Runtime Domain

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"
));

Network Domain

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 Domain

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 Domain

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 Domain

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
));

DevTools Integration

Access DevTools Instance

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()
    ));
}

BiDi Protocol Support

Access BiDi Instance

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
}

Advanced CDP Examples

Network Request Interception

// 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"
));

Cookie Management

// 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"
));

Console Message Handling

// 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

Types and Constants

// 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
}

Error Handling

Common exceptions when using CDP:

  • WebDriverException: When CDP command execution fails
  • ConnectionFailedException: When DevTools connection cannot be established
  • IllegalArgumentException: When invalid command names or parameters are provided
  • BiDiException: When BiDi protocol operations fail

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());
}

Best Practices

  1. DevTools API Preference: Use the high-level DevTools API when available instead of raw CDP commands
  2. Error Handling: Always handle potential connection failures and command errors
  3. Domain Enabling: Enable relevant CDP domains before using their commands
  4. Resource Cleanup: Disable domains when no longer needed to reduce overhead
  5. Version Compatibility: CDP commands may vary between Chrome versions; test thoroughly
  6. Performance Impact: Be mindful that extensive CDP usage can impact browser performance
  7. Security: Be cautious when disabling security features through CDP commands

CDP Command Reference

For complete CDP command documentation, refer to:

  • Chrome DevTools Protocol Documentation
  • Selenium's DevTools API for type-safe command execution
  • Chrome version-specific CDP documentation for compatibility

Install with Tessl CLI

npx tessl i tessl/maven-org-seleniumhq-selenium--selenium-chromium-driver

docs

application-launching.md

browser-configuration.md

devtools-protocol.md

index.md

media-casting.md

network-conditions.md

permissions-management.md

service-management.md

webdriver-operations.md

tile.json