How to disable logging using Selenium with Python binding

后端 未结 4 1798
误落风尘
误落风尘 2020-12-04 01:45

Simple question: how to completely disable logging when using Selenium from Python bindings, ex code as follows:

browser = webdriver.Chrome()
4条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-04 02:35

    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:

    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.

提交回复
热议问题