0
0
Selenium Pythontesting~20 mins

Browser console log capture in Selenium Python - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Browser Console Log 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 Python code snippet capturing browser console logs?
Consider the following Selenium Python code that opens a page and captures browser console logs. What will be printed?
Selenium Python
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By

options = webdriver.ChromeOptions()
options.set_capability('goog:loggingPrefs', {'browser': 'ALL'})

service = Service()
driver = webdriver.Chrome(service=service, options=options)
driver.get('data:text/html,<script>console.log("Test log");console.error("Error log");</script>')
logs = driver.get_log('browser')
for entry in logs:
    print(entry['level'], entry['message'])
driver.quit()
A
INFO data:text/html,... "Error log"
SEVERE data:text/html,... "Test log"
B
DEBUG data:text/html,... "Test log"
ERROR data:text/html,... "Error log"
C
INFO data:text/html,... "Test log"
SEVERE data:text/html,... "Error log"
DNo output because get_log('browser') returns empty list
Attempts:
2 left
💡 Hint
Browser console logs include levels like INFO and SEVERE for logs and errors respectively.
assertion
intermediate
1:30remaining
Which assertion correctly verifies that a browser console log contains an error message?
You have captured browser logs in a variable named logs (a list of dicts). Which assertion correctly checks that at least one log entry contains the text 'TypeError'?
Selenium Python
logs = [{'level': 'SEVERE', 'message': 'Uncaught TypeError: undefined is not a function'}, {'level': 'INFO', 'message': 'Page loaded'}]
Aassert logs['message'] == 'TypeError'
Bassert any('TypeError' in entry['message'] for entry in logs)
Cassert logs.contains('TypeError')
Dassert 'TypeError' in logs
Attempts:
2 left
💡 Hint
You need to check inside each log message string, not the list or dict keys directly.
locator
advanced
1:30remaining
Which locator strategy is best to find the browser console log entries in Selenium Python?
You want to capture browser console logs using Selenium Python. Which method and argument correctly retrieves these logs?
Adriver.get_console_logs()
Bdriver.get_log('console')
Cdriver.find_element(By.ID, 'browser_logs')
Ddriver.get_log('browser')
Attempts:
2 left
💡 Hint
Selenium WebDriver has a method to get logs by specifying the log type as a string.
🔧 Debug
advanced
2:00remaining
Why does this Selenium Python code fail to capture browser console logs?
Given this code snippet, why does logs = driver.get_log('browser') return an empty list?
Selenium Python
from selenium import webdriver
options = webdriver.ChromeOptions()
driver = webdriver.Chrome(options=options)
driver.get('https://example.com')
logs = driver.get_log('browser')
print(logs)
driver.quit()
ALogging preferences were not set in ChromeOptions, so no logs are captured.
BThe URL 'https://example.com' has no console logs, so empty list is expected.
Cdriver.get_log('browser') is deprecated and returns empty list.
DChromeDriver does not support capturing browser logs.
Attempts:
2 left
💡 Hint
You must enable logging preferences explicitly in ChromeOptions to capture logs.
framework
expert
3:00remaining
How to integrate browser console log capture into a pytest Selenium test with assertion?
You want to write a pytest test that opens a page, captures browser console logs, and asserts no errors are present. Which code snippet correctly implements this?
A
def test_no_console_errors(driver):
    logs = driver.get_log('browser')
    errors = [entry for entry in logs if entry['level'] == 'SEVERE']
    assert not errors, f"Console errors found: {errors}"
B
def test_no_console_errors(driver):
    logs = driver.get_log('browser')
    assert all('error' not in entry['message'].lower() for entry in logs)
C
def test_no_console_errors(driver):
    logs = driver.get_log('console')
    assert 'SEVERE' not in logs
D
def test_no_console_errors(driver):
    logs = driver.get_log('browser')
    assert logs == []
Attempts:
2 left
💡 Hint
Filter logs by level 'SEVERE' and assert the list is empty for no errors.