0
0
Selenium Pythontesting~20 mins

Network log capture in Selenium Python - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Network Log Capture Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this Selenium network log capture code?
Consider the following Python Selenium code snippet that captures network logs using Chrome DevTools Protocol (CDP). What will be printed by the last print statement?
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

options = Options()
options.add_argument('--headless=new')

caps = DesiredCapabilities.CHROME.copy()
caps['goog:loggingPrefs'] = {'performance': 'ALL'}

service = Service()
driver = webdriver.Chrome(service=service, options=options, desired_capabilities=caps)

driver.get('https://example.com')

logs = driver.get_log('performance')

count = 0
for entry in logs:
    message = entry['message']
    if 'Network.responseReceived' in message:
        count += 1

print(count)
driver.quit()
ARaises a WebDriverException because DesiredCapabilities is deprecated
BPrints the number of network responses received during page load, typically a positive integer like 5 or more
CRaises a KeyError because 'message' key does not exist in log entries
DPrints 0 because 'performance' logs are not enabled by default
Attempts:
2 left
💡 Hint
Check how performance logging is enabled and what keys exist in the log entries.
assertion
intermediate
1:30remaining
Which assertion correctly verifies a network request URL in Selenium logs?
You have captured performance logs in Selenium and want to assert that a request to 'https://api.example.com/data' was made. Which assertion code snippet correctly checks this?
Selenium Python
logs = driver.get_log('performance')
urls = []
for entry in logs:
    message = json.loads(entry['message'])['message']
    if message['method'] == 'Network.requestWillBeSent':
        urls.append(message['params']['request']['url'])
Aassert any('https://api.example.com/data' in url for url in urls)
Bassert 'https://api.example.com/data' in urls
Cassert all('https://api.example.com/data' == url for url in urls)
Dassert urls.index('https://api.example.com/data') > 0
Attempts:
2 left
💡 Hint
Check how to verify if at least one URL matches the target string.
🔧 Debug
advanced
2:30remaining
Why does this Selenium network log capture code raise a JSONDecodeError?
This code snippet tries to parse network logs but raises json.decoder.JSONDecodeError. Identify the cause.
Selenium Python
import json
logs = driver.get_log('performance')
for entry in logs:
    message = json.loads(entry['message'])
    print(message['message']['method'])
Aentry['message'] is a string containing nested JSON; json.loads parses only the outer string, inner 'message' is still a string, causing error
Bentry['message'] is not a valid JSON string, causing JSONDecodeError
Cdriver.get_log('performance') returns empty list, so loop never runs and error is elsewhere
Djson module is not imported correctly, causing JSONDecodeError
Attempts:
2 left
💡 Hint
Check the structure of the 'message' field in performance logs.
framework
advanced
2:30remaining
Which Selenium WebDriver setup correctly enables network log capture with Chrome in Python?
Select the code snippet that correctly configures Chrome WebDriver to capture network logs using performance logging.
A
from selenium import webdriver
from selenium.webdriver.chrome.service import Service

service = Service()
driver = webdriver.Chrome(service=service)
driver.execute_cdp_cmd('Network.enable', {})
B
from selenium import webdriver
from selenium.webdriver.chrome.options import Options

options = Options()
options.set_capability('goog:loggingPrefs', {'performance': 'ALL'})
options.add_argument('--headless')
driver = webdriver.Chrome(options=options)
C
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()
options.add_argument('--headless=new')

service = Service()
driver = webdriver.Chrome(service=service, options=options, desired_capabilities=caps)
D
from selenium import webdriver
from selenium.webdriver.chrome.options import Options

options = Options()
options.add_argument('--enable-logging')
driver = webdriver.Chrome(options=options)
Attempts:
2 left
💡 Hint
Check how to enable performance logging via DesiredCapabilities.
🧠 Conceptual
expert
3:00remaining
What is the main limitation of capturing network logs via Selenium's performance logging?
Choose the most accurate statement about the limitations of using Selenium's performance logging to capture network traffic.
APerformance logging captures all network traffic including WebSocket frames and browser cache hits without any filtering limitations
BPerformance logging disables browser caching, causing all resources to be fetched from the network every time
CPerformance logging requires manual parsing of raw TCP packets, making it unsuitable for HTTP-level analysis
DPerformance logging may miss some network events due to buffering and does not provide full HAR (HTTP Archive) data like dedicated proxy tools
Attempts:
2 left
💡 Hint
Consider what data Selenium's performance logs provide compared to specialized network tools.