Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: How to hide output Chrome messages in Selenium?

What I want to do:

I want to open Chrome browser using Selenium ChromeDriver without the Chrome messages getting output to the console.

What I did:

from selenium import webdriver
driver = webdriver.Chrome(r'C:\Users\u1\Documents\scripts\chromedriver.exe')

Output:

C:\Users\u1\Documents\scripts>python test.py

DevTools listening on ws://127.0.0.1:50605/devtools/browser/11c9063a-44ce-4b39-9566-9e6c6270025c

I want to hide the output message "DevTools listening on..."

What I tried to solve this:

Using contextlib

from selenium import webdriver
import contextlib

with contextlib.redirect_stdout(None):
   driver = webdriver.Chrome(r'C:\Users\u1\Documents\scripts\chromedriver.exe')

Using devnull

from selenium import webdriver
import subprocess

devnull = subprocess.DEVNULL
subprocess.Popen(open_browser(), stdout=devnull, stderr=devnull)
def open_browser():
    driver = webdriver.Chrome(r'C:\Users\u1\Documents\scripts\chromedriver.exe')

Using log-level=3

chrome_options = Options()
chrome_options.add_argument("--log-level=3")
driver = webdriver.Chrome(r'C:\Users\u1\Documents\scripts\chromedriver.exe', chrome_options=chrome_options)

But still the message is getting displayed. How do I hide the output message "DevTools listening on..." in Python?

like image 359
Dipankar Nalui Avatar asked Nov 19 '18 10:11

Dipankar Nalui


3 Answers

Add this option to your driver and your problem will be solved:

options = webdriver.ChromeOptions()<br>
options.add_experimental_option('excludeSwitches', ['enable-logging'])
like image 81
Pavan Kumar Avatar answered Oct 19 '22 23:10

Pavan Kumar


similar question in DevTools listening on ws://127.0.0.1:57671/devtools/browser/8a586f7c-5f2c-4d10-8174-7a7bf50e49b5 with Selenium and Python.

the answer is:

base on Chanticleer in hide chromeDriver console in python

Locate and edit this file as follows: located at Lib\site-packages\selenium\webdriver\common\services.py in your Python folder.

Edit the Start() function by adding the creation flags this way: creationflags=CREATE_NO_WINDOW

from win32process import CREATE_NO_WINDOW

def start(self):
    """
    Starts the Service.

    :Exceptions:
     - WebDriverException : Raised either when it can't start the service
       or when it can't connect to the service
    """
    try:
        cmd = [self.path]
        cmd.extend(self.command_line_args())
        self.process = subprocess.Popen(cmd, env=self.env,
                                        close_fds=platform.system() != 'Windows',
                                        stdout=self.log_file, stderr=self.log_file, creationflags=CREATE_NO_WINDOW)
    except TypeError:
        raise

Works Perfectly for me (python3.7, selenium 3.141.0)

please give me credit to spending hours for searching the answer.

like image 36
Wahyu Bram Avatar answered Oct 20 '22 00:10

Wahyu Bram


Those are chrome messages, so you need to set the options for the Chrome-Log Level to hide those messages, setting the log-level to --log-level=3 should be enough (only fatal log messages.

from selenium.webdriver.chrome.options import Options
[...]
chrome-options = Options()
chrome-options.add_argument("--log-level=3")
driver = webdriver.Chrome(chrome_options=chrome-options)

Also out of curiosity, might I ask why?

like image 41
Bernhard Avatar answered Oct 20 '22 00:10

Bernhard