17

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.

Rabih Kodeih
  • 9,361
  • 11
  • 47
  • 55

9 Answers9

24

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.

architux
  • 607
  • 7
  • 13
13
driver = webdriver.Chrome(service_log_path='/dev/null')
TONy.W
  • 1,948
  • 19
  • 10
8

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/null if you are running under Linux/Unix ;
  • NUL under windows

Hope it helps

Y__
  • 1,687
  • 2
  • 11
  • 23
7

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.

shtef
  • 75
  • 1
  • 4
  • 1
    I have not tested this, but I'm pretty sure you could use logpath=os.devnull and it will work independent of OS. (Need to import os, of course). – codingatty Apr 26 '19 at 01:37
  • I _have_ tested this, and it works. I was getting multi-gigabyte logs and it was killing our servers. Now, nothing at all. Thanks @codingatty – elPastor Jul 23 '19 at 21:35
  • 1
    Not working in my machine (Manjaro latest). Tried both `os.devnull` and `NUL`. My code was this `driver = webdriver.Firefox(log_path=os.devnull, options=options, capabilities=capabilities, firefox_profile=profile)` – Rahat Zaman Sep 25 '20 at 04:00
6

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

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)
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
3

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/

1

if you set service_log_path = None, it won't generate the geckodriver.log file:

driver = webdriver.Firefox(options=options, service_log_path=None)
grantr
  • 878
  • 8
  • 16
1

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);
Anglefroghammer
  • 238
  • 2
  • 15
0

You just need to find what logger is outputing the log to console then adjust it

`

    loggers = logging.Logger.manager.loggerDict

    # Print the names of all the loggers
    for name in loggers:
        print(name)
    logging.getLogger('urllib3.connectionpool').setLevel(logging.WARNING)
    logging.getLogger('selenium.webdriver.remote.remote_connection').setLevel(logging.WARNING)

`

Steve
  • 11
  • 1