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.
Network log capture in 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.
caps = DesiredCapabilities.CHROME.copy() caps['goog:loggingPrefs'] = {'performance': 'ALL'}
logs = driver.get_log('performance') for entry in logs: print(entry['message'])
This script opens a browser, navigates to example.com, captures network responses, and prints each URL with its HTTP status code.
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()
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.
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.
Practice
Solution
Step 1: Understand network logs in Selenium
Network logs capture the details of requests and responses between the browser and server during test execution.Step 2: Identify the main purpose
This helps testers verify if the correct URLs are called and if responses are successful.Final Answer:
To see the communication between browser and server during tests -> Option AQuick Check:
Network logs show browser-server communication [OK]
- Thinking network logs speed up tests
- Confusing UI changes with network logs
- Assuming network logs generate test data
Solution
Step 1: Identify the correct Chrome option for logging
Performance logging is enabled by setting "goog:loggingPrefs" with "performance" set to "ALL".Step 2: Understand other options
Other options like headless mode or disabling extensions do not enable network log capture.Final Answer:
"goog:loggingPrefs": {"performance": "ALL"} -> Option BQuick Check:
Enable performance logs with goog:loggingPrefs [OK]
- Confusing headless mode with logging
- Using download preferences for network logs
- Disabling extensions instead of enabling logs
logs = driver.get_log('performance')
for entry in logs:
message = json.loads(entry['message'])['message']
if message['method'] == 'Network.responseReceived':
print(message['params']['response']['url'])Solution
Step 1: Understand the log parsing code
The code reads performance logs, filters for 'Network.responseReceived' events, and prints the response URLs.Step 2: Identify what is printed
It prints all URLs for network responses received, regardless of success or failure.Final Answer:
All URLs of network responses received during the test -> Option CQuick Check:
Code prints URLs from Network.responseReceived events [OK]
- Assuming only failed URLs are printed
- Confusing network logs with console errors
- Thinking it lists page elements
options = webdriver.ChromeOptions()
options.set_capability('loggingPrefs', {'performance': 'ALL'})
driver = webdriver.Chrome(options=options)
logs = driver.get_log('performance')Solution
Step 1: Check capability key correctness
The correct key to enable performance logging is 'goog:loggingPrefs', not 'loggingPrefs'.Step 2: Verify other statements
ChromeOptions can be used with Chrome driver, get_log exists, and performance logs can be captured.Final Answer:
The capability key should be 'goog:loggingPrefs', not 'loggingPrefs' -> Option AQuick Check:
Use 'goog:loggingPrefs' to enable performance logs [OK]
- Using wrong capability key
- Thinking ChromeOptions is incompatible
- Believing get_log method is missing
Solution
Step 1: Capture and parse performance logs
Enable performance logging and get logs with driver.get_log('performance').Step 2: Filter 'Network.responseReceived' events and check URL and status
Look for events where response URL matches 'https://api.example.com/data' and verify status code is 200.Final Answer:
Parse performance logs, find 'Network.responseReceived' events, check if response URL matches and status is 200 -> Option DQuick Check:
Check responseReceived events for URL and status 200 [OK]
- Using browser logs instead of performance logs
- Assuming URL presence means status 200
- Trying to intercept network calls with execute_script
Ready to prove it?
Pass the test and this pattern moves unknown
