Even if you make the script as .pyw
, when the new process chromedriver.exe
is created, a console window appears for that. There is an option to turn on the option CREATE_NO_WINDOW
in C#, but there is not one yet in Python bindings for selenium. I was planning to fork selenium and add this feature myself.
Solution for now (only for Windows): Edit the selenium library
Go to this folder: C:\Users\name\AppData\Local\Programs\Python\Python38-32\Lib\site-packages\selenium\webdriver\common\
(The path till Python38-32 depends on your installation of python).
There will be a file named service.py
, which you need to edit as follows:
- Add the import statement at the top
from subprocess import STDOUT, CREATE_NO_WINDOW
- Now (maybe in the line numbers 72 to 76), you must add another option
creationflags=CREATE_NO_WINDOW
in the function subprocess.Popen()
. To make it clear, see before and after versions of code below:
Before edit:
self.process = subprocess.Popen(cmd, env=self.env,
close_fds=platform.system() != 'Windows',
stdout=self.log_file,
stderr=self.log_file,
stdin=PIPE)
After edit:
self.process = subprocess.Popen(cmd, env=self.env,
close_fds=platform.system() != 'Windows',
stdout=self.log_file,
stderr=self.log_file,
stdin=PIPE
creationflags=CREATE_NO_WINDOW) # Here !
Note:
Make a copy of the old service.py
file, so that you replace it back if you need to in the future.
Check is the existing answer on it. [old-answer](https://stackoverflow.com/questions/16180428/can-selenium-webdriver-open-browser-windows-silently-in-background?answertab=oldest#tab-top) – Suryaveer Singh Aug 25 '20 at 09:33