Beyond the Normal API
WebDriver can only do what a user can do. Sometimes you also want to watch what the browser is doing: network calls, console errors, slow connections. Selenium 4 connects to Chrome DevTools Protocol (CDP) and the newer cross-browser WebDriver BiDi (two-way) protocol for this.
Availability: CDP works in Chromium browsers (Chrome and Edge). BiDi support is growing and works in Chrome and Firefox. Check the Selenium documentation for the features of your version, because this area changes quickly.
Run Any CDP Command
python
from selenium import webdriver
driver = webdriver.Chrome()
# 1. Device emulation (mobile)
driver.execute_cdp_cmd("Emulation.setDeviceMetricsOverride", {
"width": 390, "height": 844, "deviceScaleFactor": 3, "mobile": True})
driver.get("https://www.selenium.dev")
# 2. Fake location
driver.execute_cdp_cmd("Emulation.setGeolocationOverride", {
"latitude": 28.6139, "longitude": 77.2090, "accuracy": 100})
# 3. Slow network
driver.execute_cdp_cmd("Network.enable", {})
driver.execute_cdp_cmd("Network.emulateNetworkConditions", {
"offline": False, "latency": 400, "downloadThroughput": 50_000, "uploadThroughput": 20_000})
# 4. Block ads or images
driver.execute_cdp_cmd("Network.setBlockedURLs", {"urls": ["*.png", "*doubleclick.net*"]})
Read Browser Console Logs (Chrome)
python
from selenium.webdriver.chrome.options import Options
options = Options()
options.set_capability("goog:loggingPrefs", {"browser": "ALL"})
driver = webdriver.Chrome(options=options)
driver.get("https://the-internet.herokuapp.com/javascript_error")
for entry in driver.get_log("browser"):
print(entry["level"], entry["message"])
errors = [e for e in driver.get_log("browser") if e["level"] == "SEVERE"]
assert not errors, f"JavaScript errors found: {errors}"
Great regression check: After every important page load, assert there are no SEVERE console errors. This finds hidden front-end bugs for free.
Capture a Full-Page Screenshot in Chrome
python
import base64
result = driver.execute_cdp_cmd("Page.captureScreenshot", {"captureBeyondViewport": True, "format": "png"})
with open("full_page.png", "wb") as f:
f.write(base64.b64decode(result["data"]))
Basic Auth Header
python
import base64
token = base64.b64encode(b"admin:admin").decode()
driver.execute_cdp_cmd("Network.enable", {})
driver.execute_cdp_cmd("Network.setExtraHTTPHeaders", {"headers": {"Authorization": f"Basic {token}"}})
driver.get("https://the-internet.herokuapp.com/basic_auth")
Performance and Page Weight
python
timing = driver.execute_script("return performance.timing.toJSON()")
load_ms = timing["loadEventEnd"] - timing["navigationStart"]
print("Page load time:", load_ms, "ms")
resources = driver.execute_script("return performance.getEntriesByType('resource').length")
print("Requests made:", resources)
BiDi in Selenium 4 (overview)
BiDi lets your test subscribe to browser events such as console messages or network requests instead of polling. Newer Selenium releases add BiDi helpers for logging, network interception and script evaluation in several languages. Follow the selenium.dev documentation for the exact API of your version, since it is still evolving.
| Feature | CDP | BiDi |
| Browsers | Chromium only | Cross-browser standard (growing) |
| Stability of API | Can change with Chrome | Standardized |
| Use today | Very common in projects | Preferred for new, portable code when available |
Conclusion
Use execute_cdp_cmd for emulation, network control and screenshots in Chrome, read console logs to catch JavaScript errors and watch the BiDi documentation for cross-browser equivalents.
Quick Summary: execute_cdp_cmd + get_log('browser') = powerful extras beyond normal WebDriver.