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 –
1 2 3 | options = webdriver.ChromeOptions() options.set_capability( "goog:loggingPrefs" , { "browser" : "ALL" }) |
Getting Logs
And then, we can get the logs using the below command:
1 | logs = driver.get_log( 'browser' ) |
Whole code
1 2 3 4 5 6 7 8 9 10 11 12 13 | from selenium import webdriver options = webdriver.ChromeOptions() options.set_capability( "goog:loggingPrefs" , { "browser" : "ALL" }) driver = webdriver.Chrome(options = options) # 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.
1 2 3 4 5 6 7 8 9 10 11 12 | from selenium import webdriver options = webdriver.ChromeOptions() driver = webdriver.Chrome(options = options) # 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.
1 2 3 4 5 6 7 8 9 10 11 12 | from selenium import webdriver options = webdriver.FirefoxOptions() driver = webdriver.Firefox(options = options) # 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.