2

This is my code as of now:

from selenium import webdriver

from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.service import Service

options = webdriver.ChromeOptions()
options.add_experimental_option("excludeSwitches", ["enable-logging"])

if not("-dhl" in sys.argv or "--disable-headless" in sys.argv):
    options.add_argument("--headless=new")

self.driver = webdriver.Chrome(
    service=Service(ChromeDriverManager().install()),
    options=options
)

driver.get("https://google.com")

The problem is only 1 of them will work at the same time, meaning if I try to run the browser without headless mode (run with the --disable-headless flag) the logs are turned off but if I don't do that the browser will run in headless mode but still display logs.

I've already tried using options.headless = True, disabling logs through logging module and setting it to logging.CRITICAL through the logging module and even through the selenium logging, changing the order of these 2 sections to ensure the settings aren't being overwritten but none of them seems to be the issue.

In short, I can't do both things simultaneously for some unknown reason.

3
  • This question is similar to: How to run headless Chrome with Selenium in Python?. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem. Commented Aug 19 at 19:35
  • Yes but only that's 1 part of the question my problem is I can't do both simultaneously, only one of them works at a time Commented Aug 19 at 19:47
  • 1
    Unrelated to the question, but you should stop using webdriver_manager. It is outdated and unsupported. Selenium itself provides everything you need for downloading and managing drivers: selenium.dev/documentation/selenium_manager Commented Aug 20 at 15:27

1 Answer 1

4

You need to direct ChromeDriver logs to os.devnull:

service = Service(
    ChromeDriverManager().install(),
    log_output=os.devnull
)

Then suppress the urllib3, selenium, WDM, and webdriver-manager logs, two ways to do this:

  • The normal way:

    logging.getLogger("selenium").setLevel(logging.CRITICAL)
    logging.getLogger("urllib3").setLevel(logging.CRITICAL)
    logging.getLogger("WDM").setLevel(logging.CRITICAL)
    logging.getLogger("webdriver_manager").setLevel(logging.CRITICAL)
    
  • If you want something more aggressive (filter all logs besides CRITICAL and FATAL)

    logging.getLogger().setLevel(logging.CRITICAL)
    

Also turn down Chrome's logging

options.add_argument("--log-level=3")

This should help!

Sign up to request clarification or add additional context in comments.

5 Comments

Thank very much that actually works wonderfully well out of all the answers I've tried, most of the logs are hidden now and only these 2 are left DevTools listening on ws://127.0.0.1:62248/devtools/browser/ed14dacf-69dc-46a1-866e-4b62c52801fa WARNING: All log messages before absl::InitializeLog() is called are written to STDERR I0000 00:00:1755640509.419023 6492 voice_transcription.cc:58] Registering VoiceTranscriptionCapability that I think I can bear them, would've been nice if they were to hide too tho ;)
Try sys.stderr = open(os.devnull, 'w'), now this most likely could suppress important error messages later on so you are warned. Also add options.add_argument("--disable-dev-shm-usage"), again the same, you have been warned. Glad to help!
@Ahmad, you essentially can't disable the "DevTools listening..." messages at the moment. I hope to have that fixed soon: github.com/SeleniumHQ/selenium/issues/16117
@Oro, I edited your answer. The log_path arg was replaced with log_output several years ago.
Woops, okay, I looked at the documentation and you are right. selenium.dev/documentation/webdriver/browsers/chrome/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.