0
0
Selenium Pythontesting~10 mins

Network log capture in Selenium Python - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to start capturing network logs in Selenium with Python.

Selenium Python
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options

options = Options()
options.set_capability('goog:loggingPrefs', {'performance': '[1]'})

service = Service('/path/to/chromedriver')
driver = webdriver.Chrome(service=service, options=options)
Drag options to blanks, or click blank then click option'
AALL
BERROR
COFF
DWARNING
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'ERROR' or 'OFF' disables capturing network logs.
Forgetting to set the 'goog:loggingPrefs' capability.
2fill in blank
medium

Complete the code to retrieve network logs from the Selenium driver.

Selenium Python
logs = driver.get_log('[1]')
for entry in logs:
    print(entry)
Drag options to blanks, or click blank then click option'
Aperformance
Bbrowser
Cdriver
Dclient
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'browser' or 'driver' which do not contain network logs.
Misspelling the log type string.
3fill in blank
hard

Fix the error in the code to correctly parse network log entries as JSON.

Selenium Python
import json

logs = driver.get_log('performance')
for entry in logs:
    message = json.loads(entry['[1]'])
    print(message)
Drag options to blanks, or click blank then click option'
AmessageText
Bmessage
CmessageData
DmessagePayload
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect keys like 'messageText' or 'messageData' which do not exist.
Trying to parse the entire entry instead of the 'message' field.
4fill in blank
hard

Fill both blanks to filter network requests for URLs containing 'api' and HTTP method 'GET'.

Selenium Python
filtered_requests = [msg for msg in logs if msg['message']['method'] == 'Network.requestWillBeSent' and msg['message']['params']['request']['[1]'].lower().find('api') != -1 and msg['message']['params']['request']['[2]'] == 'GET']
Drag options to blanks, or click blank then click option'
Aurl
Bmethod
Cheaders
DpostData
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'headers' or 'postData' which do not contain URL or method.
Confusing the keys and filtering incorrectly.
5fill in blank
hard

Fill all three blanks to extract the status code, URL, and response headers from network responses.

Selenium Python
for msg in logs:
    data = msg['message']['params']
    if 'response' in data:
        status = data['response']['[1]']
        url = data['response']['[2]']
        headers = data['response']['[3]']
        print(f'Status: {status}, URL: {url}, Headers: {headers}')
Drag options to blanks, or click blank then click option'
Astatus
Burl
Cheaders
DstatusText
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'statusText' instead of 'status' for the status code.
Mixing up keys or missing the 'response' key check.