Bird
Raised Fist0
Selenium Pythontesting~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of capturing browser console logs in Selenium tests?
easy
A. To find hidden errors and warnings during test execution
B. To speed up the browser loading time
C. To change the browser's user interface
D. To increase the test execution timeout

Solution

  1. Step 1: Understand browser console logs

    Browser console logs contain errors, warnings, and info messages from the browser during page load and interaction.
  2. Step 2: Purpose of capturing logs in tests

    Capturing these logs helps testers find hidden errors or warnings that are not visible on the page but affect test results.
  3. Final Answer:

    To find hidden errors and warnings during test execution -> Option A
  4. Quick Check:

    Browser logs = find hidden errors [OK]
Hint: Browser logs reveal hidden errors during tests [OK]
Common Mistakes:
  • Thinking logs speed up browser loading
  • Confusing logs with UI changes
  • Assuming logs affect timeout settings
2. Which of the following is the correct syntax to capture browser console logs in Selenium with Python?
easy
A. logs = driver.fetch_log('browser')
B. logs = driver.getLogs('console')
C. logs = driver.get_log('browser')
D. logs = driver.retrieve_log('console')

Solution

  1. Step 1: Recall Selenium Python API for logs

    The correct method to get browser console logs is driver.get_log('browser').
  2. Step 2: Check each option syntax

    Only logs = driver.get_log('browser') uses the exact correct method name and parameter. Others use incorrect method names or parameters.
  3. Final Answer:

    logs = driver.get_log('browser') -> Option C
  4. Quick Check:

    Correct method = get_log('browser') [OK]
Hint: Use driver.get_log('browser') to capture logs [OK]
Common Mistakes:
  • Using incorrect method names like getLogs or fetch_log
  • Passing wrong parameter like 'console' instead of 'browser'
  • Confusing log capture with other driver methods
3. Given the following code snippet, what will be the output if the browser console has one error and one info message?
logs = driver.get_log('browser')
for entry in logs:
    print(entry['level'])
medium
A. []
B. ["INFO", "ERROR"]
C. ["WARNING", "ERROR"]
D. ["ERROR", "INFO"]

Solution

  1. Step 1: Understand the code behavior

    The code fetches all browser logs and prints the 'level' field of each log entry in order.
  2. Step 2: Analyze given log messages

    Since the console has one error and one info message, the output will list their levels in the order they appear: "ERROR" then "INFO".
  3. Final Answer:

    ["ERROR", "INFO"] -> Option D
  4. Quick Check:

    Print log levels = ["ERROR", "INFO"] [OK]
Hint: Print log['level'] to see message types in order [OK]
Common Mistakes:
  • Assuming logs are sorted differently
  • Confusing log levels with messages
  • Expecting empty list if logs exist
4. Identify the error in this code snippet that tries to capture browser console logs:
logs = driver.get_log('console')
for log in logs:
    print(log['message'])
medium
A. Using 'log' as variable name causes conflict
B. Incorrect log type parameter; should be 'browser' instead of 'console'
C. Missing parentheses in print statement
D. No error; code is correct

Solution

  1. Step 1: Check the parameter for get_log()

    The correct parameter to get browser console logs is 'browser', not 'console'.
  2. Step 2: Verify other code parts

    Print statement syntax is correct, and variable name 'log' is valid. So no other errors.
  3. Final Answer:

    Incorrect log type parameter; should be 'browser' instead of 'console' -> Option B
  4. Quick Check:

    get_log parameter = 'browser' [OK]
Hint: Use 'browser' as parameter in get_log() [OK]
Common Mistakes:
  • Using 'console' instead of 'browser' parameter
  • Thinking print syntax is wrong in Python 3
  • Worrying about variable names unnecessarily
5. You want to capture only error-level messages from the browser console logs in Selenium Python. Which code snippet correctly filters these logs?
hard
A. logs = [entry for entry in driver.get_log('browser') if entry['level'] == 'ERROR']
B. logs = driver.get_log('browser').filter(level='ERROR')
C. logs = driver.get_log('browser').select(entry => entry.level == 'ERROR')
D. logs = driver.get_log('browser').where(entry['level'] = 'ERROR')

Solution

  1. Step 1: Understand filtering logs in Python

    Python list comprehension is used to filter logs by checking if entry['level'] equals 'ERROR'.
  2. Step 2: Analyze each option's correctness

    logs = [entry for entry in driver.get_log('browser') if entry['level'] == 'ERROR'] uses valid Python syntax. Options A, B, and C use invalid or non-Python methods like filter(), select(), or where() which do not exist on lists.
  3. Final Answer:

    logs = [entry for entry in driver.get_log('browser') if entry['level'] == 'ERROR'] -> Option A
  4. Quick Check:

    Filter logs with list comprehension [OK]
Hint: Use list comprehension to filter logs by level [OK]
Common Mistakes:
  • Using non-Python methods like filter or select on lists
  • Using single equals '=' instead of '==' in conditions
  • Trying to chain unsupported methods on get_log() result
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard