'How to disable logging using Selenium with Python binding
Simple question: how to completely disable logging when using Selenium from Python bindings, ex code as follows:
browser = webdriver.Chrome()
I've tried things like:
options = webdriver.ChromeOptions();
options.add_argument('--log-level 3')
browser = webdriver.Chrome(chrome_options=options)
or even:
options = webdriver.ChromeOptions();
options.add_argument('--disable-logging')
browser = webdriver.Chrome(chrome_options=options)
but still the file 'chromedriver.log' is appearing on each new run of the tests.
Solution 1:[1]
The source code of Chrome's webdriver, shows the existence of an option called service_log_path.
So if you want to get rid of the file, you could set this property to
/dev/nullif you are running under Linux/Unix ;NULunder windows
Hope it helps
Solution 2:[2]
You may set options.add_argument("--log-level=3") for Chrome browser to be run with Selenuim, or you may set logging level to some higher level with:
import logging
logger = logging.getLogger('selenium.webdriver.remote.remote_connection')
logger.setLevel(logging.WARNING) # or any variant from ERROR, CRITICAL or NOTSET
But some messages will appear anyway in this case, including the starting DevTools message or SSL handshake error messages.
To run Chrome browser with Selenium in console in completely silent mode, you should use this snippet:
options = Options()
options.headless = True
options.add_experimental_option("excludeSwitches", ["enable-logging"])
That trick will suppress any console message from either the Selenium driver or the browser itself, including the first message DevTools listening on ws://127.0.0.1 at the very start.
At the same time some runtime step-by-step data can be saved to service log file, in case its argument has been added.
Solution 3:[3]
driver = webdriver.Chrome(service_log_path='/dev/null')
Solution 4:[4]
Just example for Windows people:
webdriver.Firefox(log_path='NUL')
Accepted answer is correct, but if you are new to Python / windows like i am, example like this will cut you few hours of google time.
Solution 5:[5]
this worked for me:
chrome_options.add_experimental_option('excludeSwitches', ['enable-logging'])
courtesy of:
https://joshuatz.com/posts/2020/selenium-webdriver-disabling-chrome-logging-messages/
Solution 6:[6]
if you set service_log_path = None, it won't generate the geckodriver.log file:
driver = webdriver.Firefox(options=options, service_log_path=None)
Solution 7:[7]
To disable logging using Selenium and Python you need to add an experimental option through an instance of ChromeOptions() as follows:
add_experimental_option('excludeSwitches', ['enable-logging'])
Implementation
selenium4 compatible code
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
options = Options()
options.add_experimental_option('excludeSwitches', ['enable-logging'])
s = Service('C:\\BrowserDrivers\\chromedriver.exe')
driver = webdriver.Chrome(service=s, options=options)
Solution 8:[8]
I know this is old but this is still the first thing that comes up when you search for a way to prevent logging from selenium and it did not get rid of the "dev listening" messages for me and I found a way that does:
ChromeDriverService service = ChromeDriverService.CreateDefaultService();
service.HideCommandPromptWindow = true;
IWebDriver driver = new ChromeDriver(service,options);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | Mirko Daga Acevedo |
| Solution 3 | TONy.W |
| Solution 4 | shtef |
| Solution 5 | pahval rehljkov |
| Solution 6 | grantr |
| Solution 7 | undetected Selenium |
| Solution 8 | Anglefroghammer |
