When working with automated UI tests, one often overlooked but incredibly valuable aspect is browser console logs. Capturing console logs using Selenium in Python helps you identify JavaScript errors, warnings, or any custom logs that might be affecting your application’s functionality or performance.
In this article, we’ll walk through how to capture console logs in Selenium Python.
Why Capture Console Logs?
Before jumping into the how-to, let’s understand the why:
- Detect JavaScript errors: Helps in catching client-side bugs that backend tests might miss.
- Debug failed UI actions: If a test fails unexpectedly, console logs often reveal missing scripts, CORS issues, or JS crashes.
- Log custom messages: Frontend devs often log debugging messages, which can now be part of the test assertions.
Capturing console logs
To start with, let’s see how to view the console logs.
Open this link: https://testkru.com/TestUrls/TestConsoleLogs. Then, right-click anywhere on the page and select ‘Inspect‘ to open the developer tools.
And then, navigate to the Console tab to view the console logs.


You’ll notice that several logs are printed in the console. Now, let’s try to get these logs using Selenium with Python.
Set Logging Preferences
We need to enable the browser logging to start capturing the console logs –
options = webdriver.ChromeOptions()
options.set_capability("goog:loggingPrefs", {"browser": "ALL"})
Getting Logs
And then, we can get the logs using the below command:
logs = driver.get_log('browser')
Whole code
from selenium import webdriver
options = webdriver.ChromeOptions()
options.set_capability("goog:loggingPrefs", {"browser": "ALL"})
driver = webdriver.Chrome(options=options)
driver.get("https://testkru.com/TestUrls/TestConsoleLogs")
# Capture browser console logs
logs = driver.get_log('browser')
for entry in logs:
print(f"{entry['level']} - {entry['message']}")
Output of the above code
INFO - https://testkru.com/TestUrls/TestConsoleLogs 108:30 "This is a console log by using the console.log() method"
INFO - https://testkru.com/TestUrls/TestConsoleLogs 109:30 "This is an info log by using the console.info() method"
DEBUG - https://testkru.com/TestUrls/TestConsoleLogs 110:30 "This is a debug log by using the console.debug() method"
WARNING - https://testkru.com/TestUrls/TestConsoleLogs 111:29 "This is a warning log by using the console.warn() method"
SEVERE - https://testkru.com/TestUrls/TestConsoleLogs 112:29 "This is an error log by using the console.error() method"
As you can see, the code prints all types of logs – INFO, DEBUG, WARNING, and SEVERE. If needed, we can also add a check to capture only a specific type of log.
What if we don’t set the Logging Preferences?
What happens if we don’t set the goog:loggingPrefs capability? Let’s try running the program without it and see.
from selenium import webdriver
options = webdriver.ChromeOptions()
driver = webdriver.Chrome(options=options)
driver.get("https://testkru.com/TestUrls/TestConsoleLogs")
# Capture browser console logs
logs = driver.get_log('browser')
for entry in logs:
print(f"{entry['level']} - {entry['message']}")
Output –
WARNING - https://testkru.com/TestUrls/TestConsoleLogs 111:29 "This is a warning log by using the console.warn() method"
SEVERE - https://testkru.com/TestUrls/TestConsoleLogs 112:29 "This is an error log by using the console.error() method"
So, it only printed the WARNING and SEVERE logs and not the INFO and DEBUG logs.
Can we use driver.get_log(‘browser’) with Firefox as well?
The short answer is no – driver.get_log('browser') doesn’t work with Firefox, and trying to use it will result in an error.
from selenium import webdriver
options = webdriver.FirefoxOptions()
driver = webdriver.Firefox(options=options)
driver.get("https://testkru.com/TestUrls/TestConsoleLogs")
# Capture browser console logs
logs = driver.get_log('browser')
for entry in logs:
print(f"{entry['level']} - {entry['message']}")
Output –
selenium.common.exceptions.WebDriverException: Message: HTTP method not allowed
Now, you can make an informed decision on whether to set the capability or not depending on your requirement.
This is it. We hope that you have liked the article. If you have any doubts or concerns, please write to us in the comments or mail us at admin@codekru.com.

I’m trying to capture browser console logs using Selenium + Pytest, and here’s my current code snippet:
“`
def test_capture_console_logs_inbuilt(driver, capture_browser_logs):
driver.get(“https://www.google.com”)
# Execute JavaScript to log information in the browser console
script = “””
console.log(‘Info: This is a test log.’);
console.warn(‘Warning: This is a warning log.’);
console.error(‘Error: This is an error log.’);
“””
driver.execute_script(script)
assert False
@pytest.fixture(scope=”function”)
def capture_browser_logs(driver):
yield
print(“\n=== Attempting to capture browser logs ===”)
try:
browser_logs = driver.get_log(“browser”)
print(f”Successfully retrieved {len(browser_logs)} log entries”)
if browser_logs:
for entry in browser_logs:
print(f”[{entry[‘level’]}] {entry[‘message’]}”)
except Exception as e:
print(f”Error retrieving logs: {e}”)
print(“=== End capture attempt ===”)
“`
When I run with “assert True” –> The test passes, and I can see that the browser logs are captured successfully.
“`
=== Attempting to capture browser logs ===
Successfully retrieved 3 log entries
[INFO] console-api 3:16 “Info: This is a test log.”
[WARNING] console-api 4:16 “Warning: This is a warning log.”
[SEVERE] console-api 5:16 “Error: This is an error log.”
=== End capture attempt ===
“`
When I run with “assert False” –> The test fails as expected, but the browser logs are empty (i.e., an empty array is returned).
“`
=== Attempting to capture browser logs ===
Successfully retrieved 0 log entries
=== End capture attempt ===
“`
Q: Why are the browser logs empty when the test fails?
Can you please share the full code snippet