Introduction
Modern web applications constantly exchange data with servers through network requests. Every time a webpage loads, the browser downloads HTML files, CSS stylesheets, JavaScript files, images, fonts, APIs, and other resources. During automation testing, there are situations where you may want to monitor, modify, or block some of these network requests to simulate different conditions.
Chrome DevTools Protocol (CDP) provides powerful network interception capabilities that allow Selenium to interact directly with the browser’s network layer. This enables automation engineers to observe requests, block specific resources, improve test execution speed, and simulate various network scenarios.
In this tutorial, you’ll learn how to enable network tracking using CDP, block specific resource types such as images, understand how network interception works, and explore its practical applications in Selenium automation.
What is Network Interception?
Network Interception is the process of monitoring, modifying, or blocking network requests made by the browser while loading a webpage.
Using Chrome DevTools Protocol, Selenium can intercept browser requests before they are processed, allowing automation scripts to control how web resources are loaded.
For example, you can:
Block image requests.
Monitor API calls.
Analyze network traffic.
Simulate slow network conditions.
Test application behavior when certain resources are unavailable.
Why Use Network Interception?
Network interception helps automation engineers to:
Improve automation execution speed.
Reduce unnecessary resource downloads.
Test applications under different network conditions.
Validate API requests.
Debug loading issues.
Analyze browser network activity.
Simulate missing resources during testing.
It is especially useful for performance testing and advanced browser automation.
How Selenium Uses CDP for Network Interception
Selenium communicates with Chrome DevTools using the execute_cdp_cmd() method.
Before monitoring or blocking requests, the browser’s network domain must be enabled.
General Syntax:
driver.execute_cdp_cmd(
"Network.enable",
{}
)
driver.execute_cdp_cmd(
"Network.setBlockedURLs",
{
"urls": ["pattern"]
}
)
Network.enable activates network tracking.
Network.setBlockedURLs blocks requests that match the specified URL patterns.
Example
from selenium import webdriver
# Topic: 37. Chrome DevTools Protocol (CDP) - Network Interception
# Practice site: https://www.testmuai.com/selenium-playground/simple-form-demo
# Run: pytest -s 37_examples/test_02_network_interception.py
#
# Network.enable activates network tracking through CDP so you can observe or
# block requests while the page loads.
def test_network_interception():
driver = webdriver.Chrome()
try:
driver.execute_cdp_cmd("Network.enable", {})
driver.execute_cdp_cmd(
"Network.setBlockedURLs",
{"urls": ["*.png", "*.jpg", "*.jpeg", "*.gif", "*.svg"]},
)
driver.get("https://www.testmuai.com/selenium-playground/simple-form-demo")
assert "Simple Form Demo" in driver.title or "simple-form" in driver.current_url
finally:
driver.quit()
Understanding the Code
Import Required Library
from selenium import webdriver
Imports the Selenium WebDriver module required to launch and control the Chrome browser.
Create a Chrome Browser Instance
driver = webdriver.Chrome()
Starts a new Chrome browser session.
Modern Selenium automatically manages the appropriate ChromeDriver through Selenium Manager.
Enable Network Tracking
driver.execute_cdp_cmd(
"Network.enable",
{}
)
Activates Chrome DevTools’ network domain.
Once enabled, Selenium can monitor and control browser network activity using CDP commands.
Block Image Requests
driver.execute_cdp_cmd(
"Network.setBlockedURLs",
{
"urls": [
"*.png",
"*.jpg",
"*.jpeg",
"*.gif",
"*.svg"
]
}
)
Instructs Chrome to block requests for common image file formats.
As a result:
PNG images are blocked.
JPG and JPEG images are blocked.
GIF images are blocked.
SVG images are blocked.
The webpage still loads, but these image resources are not downloaded.
Open the Practice Website
driver.get(
"https://www.testmuai.com/selenium-playground/simple-form-demo"
)
Navigates to the practice page.
Since image requests are blocked, the browser skips downloading the specified image resources, which may slightly reduce page load time.
Verify the Page Loaded Successfully
assert (
"Simple Form Demo" in driver.title
or
"simple-form" in driver.current_url
)
Verifies that the webpage loaded successfully by checking either:
The page title, or
The current URL.
If neither condition is true, the test fails.
Close the Browser
driver.quit()
Closes all browser windows and ends the WebDriver session.
Always close the browser after test execution to free system resources.
Practical Example
Suppose your application contains hundreds of product images on a catalog page.
During functional testing, image rendering is not important.
By blocking image requests, the automation script loads the page faster, reducing overall test execution time.
Automation Testing Example
Consider an e-commerce website.
The automation script:
Enables network interception.
Blocks all image downloads.
Opens the product listing page.
Performs search and filtering operations.
Verifies product information without waiting for images to load.
This improves the speed and efficiency of automated regression tests.
Real-World Example
Network interception is commonly used in:
Performance testing
API request validation
Browser optimization
Image blocking during regression testing
Simulating missing resources
Progressive Web App (PWA) testing
Enterprise automation frameworks
Debugging network-related issues
Many organizations use CDP network interception to accelerate large automation suites by preventing unnecessary resource downloads.
Advantages of Network Interception
Improves automation execution speed.
Reduces unnecessary network traffic.
Enables monitoring of browser requests.
Allows blocking specific resource types.
Helps simulate various network conditions.
Useful for debugging network-related issues.
Supports advanced browser automation scenarios.
Common Mistakes Beginners Make
Forgetting to Enable the Network Domain
Network-related CDP commands require Network.enable to be executed first.
Without enabling the network domain, many network commands will not work as expected.
Blocking Essential Resources
Blocking JavaScript or CSS files may prevent the webpage from functioning correctly.
Only block resources that are unnecessary for the current test scenario.
Assuming Network Interception Works in All Browsers
CDP network interception is designed for Chromium-based browsers such as Google Chrome and Microsoft Edge.
Support may differ in non-Chromium browsers.
Blocking Too Many Resources
Blocking excessive resources can change webpage behavior and produce unreliable test results.
Only block what is required for your test.
Best Practices
Enable the network domain before using network commands.
Block only unnecessary resources such as images when appropriate.
Verify that the webpage still functions correctly after blocking resources.
Use wildcard patterns carefully to avoid blocking essential files.
Keep network interception logic separate from business test logic.
Combine network interception with performance testing when required.
Conclusion
Network interception allows Selenium to monitor and control browser network activity using Chrome DevTools Protocol. By enabling the network domain and blocking selected resources, automation engineers can improve test performance, simulate different browser conditions, and gain greater control over how web applications are loaded. It is a valuable capability for modern Selenium automation, especially in enterprise testing environments.
Frequently Asked Questions (FAQs)
What is Network Interception?
Network interception is the process of monitoring, modifying, or blocking network requests made by the browser while loading a webpage.
Which CDP command enables network monitoring?
Use:
driver.execute_cdp_cmd(
"Network.enable",
{}
)
How can Selenium block image requests?
Use:
driver.execute_cdp_cmd(
"Network.setBlockedURLs",
{
"urls": [
"*.png",
"*.jpg",
"*.jpeg",
"*.gif",
"*.svg"
]
}
)
Why block images during automation testing?
Blocking images can reduce page load time and improve the execution speed of automation tests when image rendering is not being validated.
Where is Network Interception commonly used?
It is commonly used for performance testing, API monitoring, debugging, browser optimization, and advanced Selenium automation.
Key Takeaways
Network Interception allows Selenium to monitor and control browser network requests using Chrome DevTools Protocol.
Network.enable activates the browser’s network domain.
Network.setBlockedURLs blocks requests that match specified URL patterns.
Blocking unnecessary resources such as images can improve automation execution speed.
Network interception is useful for performance testing, debugging, API validation, and enterprise automation frameworks.
Use network interception carefully to avoid blocking resources that are essential for the application’s functionality.
