Overview
Zendriver provides full access to the Chrome DevTools Protocol (CDP), allowing you to send custom commands and listen to events. While Zendriver includes high-level methods for common operations, CDP gives you fine-grained control over the browser.Basic usage
All CDP commands are sent using thesend() method on a tab object. Commands are organized into domains like runtime, network, page, etc.
Available CDP domains
Zendriver includes all CDP domains. Here are some commonly used ones:Runtime
Execute JavaScript, handle console messages, and manage execution contexts
Network
Monitor and intercept network requests and responses
Page
Control page lifecycle, navigation, and capture screenshots
DOM
Query and manipulate the DOM tree
Fetch
Intercept and modify network requests at the protocol level
Emulation
Emulate different devices, viewports, and user agents
Listening to CDP events
You can add handlers to listen for CDP events. This is useful for monitoring console messages, network activity, and other browser events.Always enable the CDP domain before adding handlers for its events. For example, call
cdp.runtime.enable() before listening to ConsoleAPICalled events.Common CDP patterns
Execute JavaScript
Usecdp.runtime.evaluate() to execute JavaScript in the page context:
Monitor network requests
Set device emulation
Capture full page screenshot
CDP reference documentation
For a complete list of CDP commands, events, and types, refer to:- Chrome DevTools Protocol Documentation
- Zendriver’s CDP module:
zendriver/cdp/
Best practices
Enable domains before use
Always enable a CDP domain before using its commands or listening to its events.
Use high-level methods first
Check if Zendriver provides a high-level method before using CDP directly. They’re often easier to use and more reliable.
Troubleshooting
Command not working
Command not working
Make sure you’ve enabled the appropriate CDP domain first. For example,
cdp.network.enable() must be called before network-related commands work.Events not firing
Events not firing
Verify that:
- The domain is enabled
- The handler is added before the event occurs
- The event signature matches the CDP specification
Type errors with CDP parameters
Type errors with CDP parameters
CDP types are strictly typed. Check the CDP documentation for the correct parameter types and required fields.
Next steps
Request interception
Learn how to intercept and modify network requests
Anti-detection
Configure Zendriver to avoid bot detection