0

I have a Python script that picks a different song playlist and opens it in the browser on YouTube. However, every time I run it, I get a lot of messages in the console. I don't want this. How can I prevent it? I am wanting to prevent only Selenium messages, if possible, but all console messages is fine.

I've tried adding the argument:

options = webdriver.ChromeOptions()
options.add_argument("--log-level=OFF")

Originally, I had this line of code - I can't remember why it was needed, but deleting it seemed to do nothing:

options.add_experimental_option('excludeSwitches', ['enable-logging'])

I tried calling the driver with these parameters:

driver = webdriver.Chrome(service_log_path=os.devnull) driver = webdriver.Chrome(service_log_path='NUL')

And here is what I'm getting in the console:

DevTools listening on ws://127.0.0.1:55146/devtools/browser/9ffb59e1-adc0-480e-835e-e36ffe7cb428

DevTools listening on ws://127.0.0.1:55156/devtools/browser/5b6b567e-6ae1-4cdd-8206-915e788e6fe0
[8812:12068:0207/132732.310:ERROR:device_event_log_impl.cc(215)] [13:27:32.309] USB: usb_service_win.cc:415 Could not read device interface GUIDs: The system cannot find the file specified. (0x2)
[8812:12068:0207/132732.310:ERROR:device_event_log_impl.cc(215)] [13:27:32.310] USB: usb_device_handle_win.cc:1046 Failed to read descriptor from node connection: A device attached to the system is not functioning. (0x1F)
Traceback (most recent call last):
  File "c:\Users\EliBa\OneDrive\Documents\Coding Projects\Python\musico.py", line 14, in <module>
    driver.fullscreen_window()
  File "C:\Users\EliBa\AppData\Local\Programs\Python\Python311\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 596, in fullscreen_window        
    self.execute(Command.FULLSCREEN_WINDOW)
  File "C:\Users\EliBa\AppData\Local\Programs\Python\Python311\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 440, in execute
    self.error_handler.check_response(response)
  File "C:\Users\EliBa\AppData\Local\Programs\Python\Python311\Lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 245, in check_response        
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchWindowException: Message: no such window: target window already closed
from unknown error: web view not found
  (Session info: chrome=109.0.5414.120)
Stacktrace:
Backtrace:
        (No symbol) [0x002B6643]
        (No symbol) [0x0024BE21]
        (No symbol) [0x0014DA9D]
        (No symbol) [0x0012EF6A]
        (No symbol) [0x001A3AAB]
        (No symbol) [0x001B61B6]
        (No symbol) [0x0019FB76]
        (No symbol) [0x001749C1]
        (No symbol) [0x00175E5D]
        GetHandleVerifier [0x0052A142+2497106]
        GetHandleVerifier [0x005585D3+2686691]
        GetHandleVerifier [0x0055BB9C+2700460]
        GetHandleVerifier [0x00363B10+635936]
        (No symbol) [0x00254A1F]
        (No symbol) [0x0025A418]
        (No symbol) [0x0025A505]
        (No symbol) [0x0026508B]
        BaseThreadInitThunk [0x76CC7D69+25]
        RtlInitializeExceptionChain [0x77E2BB9B+107]
        RtlClearBits [0x77E2BB1F+191]
        (No symbol) [0x00000000]

Nothing has worked, any suggestions?

Eli Bauer
  • 29
  • 5

2 Answers2

0

There are two parts to this, depending on what level of logging you wish to retain.

from selenium.webdriver.chrome.service import Service as ChromeService # Similar thing for firefox also!
from subprocess import CREATE_NO_WINDOW

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

OR/AND

chrome_service = ChromeService()
chrome_service.creationflags = CREATE_NO_WINDOW

driver = webdriver.Chrome(options=options,  service=chrome_service)

The options set to log level 3 will only allow certain logs to come from the driver. However, if you have chrome_service.creationflags = CREATE_NO_WINDOW it will not send ANY information from the driver to the python console, so in truth the only one you need is chrome_service.creationflags = CREATE_NO_WINDOW but that might be too much, if so, id use options.add_argument("--log-level=3") as that will only allow few messages through and they will all usually be relevant.

Below are images of me navigating to the same place using each option & none for comparison(ignore the boxes with red border, they are irrelevant):

options.add_argument("--log-level=3") Log level 3 only showing Dev Tools Listening....

chrome_service.creationflags = CREATE_NO_WINDOW enter image description here

Neither option enter image description here

You can see how having no option is throwing me all sort of errors for bluetooth adaptors and certificates but with either option selected, the output is alot more controlled. My suggestion is to use log level 3.

Isaac M
  • 56
  • 4
0

CREATE_NO_WINDOW

CREATE_NO_WINDOW process creation flag indicates the process as a console application that is being run without a console window. Therefore, the console handle for the application is not set. This flag is ignored if the application is not a console application, or if it is used with either CREATE_NEW_CONSOLE or DETACHED_PROCESS.


This usecase

To supress the Selenium console output completely you can use the CREATE_NO_WINDOW process creation flag as follows:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
import subprocess
from subprocess import CREATE_NO_WINDOW

options = Options()
options.add_argument("start-maximized")
s = Service('C:\\BrowserDrivers\\chromedriver.exe')
s.creationflags = subprocess.CREATE_NO_WINDOW
driver = webdriver.Chrome(service=s, options=options)
driver.get('https://www.google.com/')

Console snapshot:

CREATE_NO_WINDOW

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Is there a way to also get rid of the "DevTools" message as well? I don't want anything at all to show up in the console, best case scenario. – Eli Bauer Feb 07 '23 at 23:47
  • What is wrong with this code? ``` from selenium import webdriver from selenium.webdriver.chrome.options import Options from selenium.webdriver.chrome.service import Service import subprocess from subprocess import CREATE_NO_WINDOW import math import random options = Options() options.add_argument("start-maximized") s = Service('C:\\BrowserDrivers\\chromedriver.exe') s.creationflags = subprocess.CREATE_NO_WINDOW driver = webdriver.Chrome(service=s, options=options) driver.get(m[math.floor(random.random()*len(m))]) ``` – Eli Bauer Feb 08 '23 at 00:09