6
var driverService = ChromeDriverService.CreateDefaultService();
driverService.HideCommandPromptWindow = true;

var driver = new ChromeDriver(driverService, new ChromeOptions());

Is it possible to achieve this in Python? I've tried all the possible fixes posted here on StackoverFlow and outside, but nothing when I run my .exe, the cmd appears too.

Python 3.6, latest Selenium version (3.9).

Cœur
  • 37,241
  • 25
  • 195
  • 267
masc
  • 243
  • 1
  • 2
  • 12
  • instead of renaming the question as "SOLVED -", you must check mark the right answer – Guilherme Feb 16 '18 at 20:40
  • Does this answer your question? [hide chromeDriver console in python](https://stackoverflow.com/questions/33983860/hide-chromedriver-console-in-python) – Ali Sajjad Feb 12 '22 at 15:11

4 Answers4

10

I found the way to replicate the above code in python (more or less) Used this fix as base to my inspiration.

After hours of struggling (and also really bad words), I've made this commit on github bywhich console prompt behaviour is now easily changable via code. I will try to make it available in the official sources. Hope it will make you save time and patience.

STEP 1

Locate service.py, generally in "X:\YourPythonFold\Lib\site-packages\selenium\webdriver\common\service.py"

STEP 2

Replace these lines (n° 72-76 approximately, below start method def):

self.process = subprocess.Popen(cmd, env=self.env,
                                            close_fds=platform.system() != 'Windows',
                                            stdout=self.log_file,
                                            stderr=self.log_file,
                                            stdin=PIPE)

with

if any("hide_console" in arg for arg in self.command_line_args()):
                self.process = subprocess.Popen(cmd, stdin=PIPE, stdout=PIPE, stderr=PIPE, creationflags=0x08000000)
            else:
                self.process = subprocess.Popen(cmd, env=self.env, close_fds=platform.system() != 'Windows', stdout=self.log_file, stderr=self.log_file, stdin=PIPE)

Finally in your code, when you setup your driver (I chose Chrome as example):

args = ["hide_console", ]
driver = webdriver.Chrome("your-path-to-chromedriver.exe", service_args=args, ...)

When edit the source code, be careful to PEP! Do not use tabs, just spaces!

masc
  • 243
  • 1
  • 2
  • 12
  • 1
    I found that keeping the close_fds=platform.system() != 'Windows' parameter ensures that the IEDriverServer.exe process is always closed when your application is closed. This may preferred, since otherwise you end up with multiple invisible processes. – eliteproxy Jul 31 '18 at 14:46
  • tanks It was fully functional on pyqt – alireza jahani May 09 '23 at 11:09
3

This feature has been added in python library since selenium4 release! See this answer

from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService # Similar thing for firefox also!
from subprocess import CREATE_NO_WINDOW # This flag will only be available in windows

# Define your own service object with the `CREATE_NO_WINDOW ` flag
# If chromedriver.exe is not in PATH, then use:
# ChromeService('/path/to/chromedriver')
chrome_service = ChromeService('chromedriver')
chrome_service.creationflags = CREATE_NO_WINDOW

driver = webdriver.Chrome(service=chrome_service)
Ali Sajjad
  • 3,589
  • 1
  • 28
  • 38
0

here is the most basic and simplest solution:

service = ChromeService('msedgedriver.exe')
service.creation_flags = 0x08000000
CrotaL
  • 19
  • 5
-1

I found a work-around for this problem: don't use the -w or --Windowed pyinstaller option. In other words, do this:

pyinstaller --onefile "main.py"

and not this:

pyinstaller --onefile -w "main.py"

Yes, you will get a command window at the beginning, but you don't get the window popping up every time the geckodriver.exe is called. I was struggling with firefox, hence geckodriver.exe

S. Wong
  • 59
  • 6