Network log capture helps you see what data your browser sends and receives during testing. It shows requests and responses, so you can check if your app talks to servers correctly.
0
0
Network log capture in Selenium Python
Introduction
You want to check if a web page loads all images and scripts properly.
You need to verify that API calls return the right data during a test.
You want to find out why a page is slow by looking at network requests.
You want to confirm that sensitive data is not sent over insecure connections.
Syntax
Selenium Python
from selenium import webdriver from selenium.webdriver.chrome.service import Service from selenium.webdriver.chrome.options import Options from selenium.webdriver.common.desired_capabilities import DesiredCapabilities caps = DesiredCapabilities.CHROME.copy() caps['goog:loggingPrefs'] = {'performance': 'ALL'} options = Options() service = Service('/path/to/chromedriver') driver = webdriver.Chrome(service=service, options=options, desired_capabilities=caps) # After loading a page logs = driver.get_log('performance')
Use desired_capabilities to enable performance logging in Chrome.
Logs contain network events in JSON format inside the 'message' field.
Examples
This sets Chrome to capture all performance logs, including network events.
Selenium Python
caps = DesiredCapabilities.CHROME.copy() caps['goog:loggingPrefs'] = {'performance': 'ALL'}
This retrieves and prints all performance log messages from the browser.
Selenium Python
logs = driver.get_log('performance') for entry in logs: print(entry['message'])
Sample Program
This script opens a browser, navigates to example.com, captures network responses, and prints each URL with its HTTP status code.
Selenium Python
from selenium import webdriver from selenium.webdriver.chrome.service import Service from selenium.webdriver.chrome.options import Options from selenium.webdriver.common.desired_capabilities import DesiredCapabilities import json caps = DesiredCapabilities.CHROME.copy() caps['goog:loggingPrefs'] = {'performance': 'ALL'} options = Options() service = Service('/path/to/chromedriver') driver = webdriver.Chrome(service=service, options=options, desired_capabilities=caps) try: driver.get('https://example.com') logs = driver.get_log('performance') network_events = [] for entry in logs: message = json.loads(entry['message'])['message'] if 'Network.responseReceived' == message['method']: url = message['params']['response']['url'] status = message['params']['response']['status'] network_events.append(f"URL: {url}, Status: {status}") for event in network_events: print(event) finally: driver.quit()
OutputSuccess
Important Notes
Make sure to use the correct path to your ChromeDriver in Service.
Network logs can be large; filter them to find useful info.
Not all browsers support performance logging the same way; Chrome is recommended.
Summary
Network log capture lets you see browser-server communication during tests.
Enable performance logging in Chrome to get network events.
Parse logs to check URLs and response status codes for validation.