Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'ERROR' or 'OFF' disables capturing network logs.
Forgetting to set the 'goog:loggingPrefs' capability.
✗ Incorrect
Setting 'performance' logging preference to 'ALL' enables capturing all network logs.
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'browser' or 'driver' which do not contain network logs.
Misspelling the log type string.
✗ Incorrect
Network logs are stored under the 'performance' log type in Selenium.
3fill in blank
hardFix 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'
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.
✗ Incorrect
The network log JSON string is stored in the 'message' key of each log entry.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'headers' or 'postData' which do not contain URL or method.
Confusing the keys and filtering incorrectly.
✗ Incorrect
The 'url' key contains the request URL, and 'method' contains the HTTP method.
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'statusText' instead of 'status' for the status code.
Mixing up keys or missing the 'response' key check.
✗ Incorrect
The response dictionary contains 'status', 'url', and 'headers' keys for these values.