0
0
Selenium Pythontesting~15 mins

Browser console log capture in Selenium Python - Build an Automation Script

Choose your learning style9 modes available
Capture and verify browser console logs on page load
Preconditions (3)
Step 1: Open the browser and navigate to 'https://example.com'
Step 2: Wait until the page is fully loaded
Step 3: Capture the browser console logs
Step 4: Check if there are any error messages in the console logs
✅ Expected Result: Browser console logs are captured successfully and no error messages are found
Automation Requirements - Selenium with Python
Assertions Needed:
Assert that console logs are not empty
Assert that no console log entry has level 'SEVERE' (error)
Best Practices:
Use explicit waits to ensure page load
Use appropriate log type 'browser' to capture console logs
Use try-except blocks to handle exceptions gracefully
Automated Solution
Selenium Python
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.chrome.options import Options
import unittest

class TestBrowserConsoleLogs(unittest.TestCase):
    def setUp(self):
        chrome_options = Options()
        chrome_options.set_capability('goog:loggingPrefs', {'browser': 'ALL'})
        self.driver = webdriver.Chrome(options=chrome_options)
        self.wait = WebDriverWait(self.driver, 10)

    def test_capture_console_logs(self):
        self.driver.get('https://example.com')
        # Wait for the page title to be present as indication of load
        self.wait.until(EC.title_contains('Example Domain'))

        logs = self.driver.get_log('browser')
        self.assertTrue(len(logs) > 0, 'Console logs should not be empty')

        severe_errors = [entry for entry in logs if entry['level'] == 'SEVERE']
        self.assertEqual(len(severe_errors), 0, f'Found SEVERE errors in console logs: {severe_errors}')

    def tearDown(self):
        self.driver.quit()

if __name__ == '__main__':
    unittest.main()

The setUp method configures Chrome to capture browser console logs by setting the goog:loggingPrefs capability.

The test navigates to https://example.com and waits explicitly for the page title to confirm the page loaded.

It then retrieves the browser console logs using get_log('browser').

Assertions check that logs are not empty and that no log entry has the level SEVERE, which indicates errors.

The tearDown method closes the browser to clean up.

This structure follows unittest framework conventions and uses explicit waits for reliability.

Common Mistakes - 4 Pitfalls
{'mistake': 'Not setting logging preferences in Chrome options', 'why_bad': 'Without setting logging preferences, browser console logs will not be captured.', 'correct_approach': "Set 'goog:loggingPrefs' capability with {'browser': 'ALL'} in Chrome options before creating the driver."}
Using implicit waits or no waits before capturing logs
{'mistake': 'Checking logs without filtering by log level', 'why_bad': 'Not filtering can cause false positives or miss critical errors.', 'correct_approach': "Filter logs by 'SEVERE' level to detect actual errors."}
{'mistake': 'Using incorrect log type name in get_log()', 'why_bad': "Using wrong log type (e.g., 'driver' instead of 'browser') returns empty or irrelevant logs.", 'correct_approach': "Use 'browser' as the log type to capture console logs."}
Bonus Challenge

Now add data-driven testing to capture console logs for these URLs: 'https://example.com', 'https://www.wikipedia.org', 'https://www.python.org'

Show Hint