6

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?

Andrew Myers
  • 2,754
  • 5
  • 32
  • 40
Dipankar Nalui
  • 1,121
  • 5
  • 18
  • 33

5 Answers5

12

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

options = webdriver.ChromeOptions()<br>
options.add_experimental_option('excludeSwitches', ['enable-logging'])
William Baker Morrison
  • 1,642
  • 4
  • 21
  • 33
Pavan Kumar
  • 121
  • 1
  • 3
7

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.

Wahyu Bram
  • 413
  • 1
  • 7
  • 13
  • 3
    If you find a question with a similar answer, the proper procedure is to leave a comment. You will need to earn a bit of reputation before you can do that, though. Please refrain from posting duplicate answers in the meantime. – tripleee Mar 25 '19 at 12:39
  • For me Full path is C:\Users\u1\AppData\Local\Programs\Python\Python37\Lib\site-packages\selenium\webdriver\common\service.py – Dipankar Nalui Mar 25 '19 at 14:14
  • It is service.py not services.py for me – Dipankar Nalui Mar 25 '19 at 14:14
  • I did as you answered. But I see this error `from win32process import CREATE_NO_WINDOW ModuleNotFoundError: No module named 'win32process'` – Dipankar Nalui Mar 25 '19 at 14:16
  • `pip install win32process` Gives me this error `Collecting win32process Could not find a version that satisfies the requirement win32process (from versions: ) No matching distribution found for win32process` – Dipankar Nalui Mar 25 '19 at 14:16
  • Here is how to overcome the above issue with `win32process` https://stackoverflow.com/a/42400980/4170720 – Dipankar Nalui Mar 25 '19 at 14:23
  • when import win32process error, means, you don't have the module. you must install it first before you can import it. just follow the link that given by @Dipankar Nalui – Wahyu Bram Mar 31 '19 at 15:23
  • **Solutions:** Hope this will help you all: Path: `Lib\site-packages\selenium\webdriver\common\service.py` (Not services.py, its service.py) To fix import error, use the following: `from subprocess import CREATE_NO_WINDOW` (This will **not work with Python 3.6 or older**) For older python, use `CREATE_NO_WINDOW= 0x8000000` (**For python 3.6 or older**) – Sam Apr 06 '21 at 05:00
3

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?

Bernhard
  • 1,253
  • 8
  • 18
  • I already used this code. That message is still displayed. This does not solve my problem. – Dipankar Nalui Nov 19 '18 at 10:49
  • 2
    I googled and found this same solution earlier. Many people suggested this solution in stackoverflow. But this did not solve my problem. So, I wanted to solve it in a different way. – Dipankar Nalui Nov 19 '18 at 10:51
  • Thought that would help. btw I'd put the fact that you found a "solution" that worked for somebody else and that it did not work for you in the question with a link. – Bernhard Nov 19 '18 at 12:01
  • this does not hide the message, neither does loglevel 0, 1 ,2 , 3 or 4 – scavenger Sep 05 '20 at 20:39
  • @Dipankar you're right in that it doesn't hide "DevTools listening on ws://..." message but it still hides some other logs. – YoussefDir Dec 02 '22 at 06:18
0

passing --show-capture=no does the job

mirhossein
  • 682
  • 7
  • 16
-2

There is a way to modify the library selenium to solve this problem.

you can search directly with the next route, for modify the lines the that subprocess, this close the message that apper in the console.

you go to main folder In the route /venv/selenium/webdriver/common/service.py you modify the next lines, please you write this.

cmd = [self.path]
cmd.extend(self.command_line_args())
self.process = subprocess.Popen(cmd, stdin=PIPE, stdout=PIPE, 
                                stderr=PIPE, shell=False,
                               creationflags=0x08000000)

with this piece of code you will get the solution.

Elq_eng
  • 1
  • 3
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Apr 26 '22 at 14:01