selenium hide chromdriver console window
When you run the pyinstaller command, try chainging it to this: pyinstaller ./main.py --onefile --noconsole --add-binary "chromedriver path;./driver"
--onefile
It outputs as one .exe
.
--noconsole
The console is not displayed when the created exe is executed.
--add-binary
Specify the binary file you want to add to the exe. The specification method is "source_file_path; destination_file_path"
. In this case, we want to add the chromedriver file.
When using the --onefile
option, the binary file specified with --add-binary
is included in the exe. They are expanded to a temporary folder at runtime. Therefore, using relative paths in the Python source may not work.
For example, it is necessary to rewrite the part using the relative path as follows:
driver = webdriver.Chrome('./driver/chromedriver.exe')
Rewrite as follows:import os
import sys
def resource_path(relative_path):
try:
base_path = sys._MEIPASS
except Exception:
base_path = os.path.dirname(__file__)
return os.path.join(base_path, relative_path)
driver = webdriver.Chrome(resource_path('./driver/chromedriver.exe'))
Add the function resource_path
to use it.resource_path
gets the path with the following logic:When executing from exe, get the relative path from sys._MEIPASS (when executing from exe, the temporary folder path is entered).
When executing from Python, get the relative path from this file (file).
Now it works properly whether you run it from Python or an exe file.
IF NONE OF THAT WORKS, TRY THIS IN YOUR CODE:
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
chrome_service = ChromeService('chromedriver')
chrome_service.creationflags = CREATE_NO_WINDOW # We change the property in Service class, default was 0
driver = webdriver.Chrome(service=chrome_service)
This above code will only work on selenium version 4.0.0
or higher. If you don't have this version, try this pip command: pip install selenium=4.0.0a7
Then, the program works, but the chromedriver window doesn't show up!
How to hide Chromedriver console window?
You need to call driver.quit()
at the end of the script:
If you want to just close the service executable and let the browser stay opened, call:
quit()
Closes the browser and shuts down the ChromeDriver executable
that is started when starting the ChromeDriver
driver.service.stop()
FYI, I've figured this out from the quit()
method implementation (source code). How to hide Chrome Driver in python?
REF: how-could-i-start-a-selenium-browserlike-firefox-minimized
You can move browser window over the monitor, like this:
driver.set_window_position(-10000,0)
How I can hide chromedriver log on console through selenium p
In Lib\site-packages\selenium\webdriver\common\services.py file
In Service class
in func start add creationflags argument
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=0x08000000,
stdin=PIPE)
except TypeError: ...
ChromeDriver console application hide
Yes, you need modify source code in WebDriver\DriverService.cs in Start()
; add:
this.driverServiceProcess.StartInfo.CreateNoWindow = true;
Related Topics
Differencebetween Np.Array() and Np.Asarray()
How to Know/Change Current Directory in Python Shell
How to Get the Version Defined in Setup.Py (Setuptools) in My Package
Cannot Redirect Output When I Run Python Script on Windows Using Just Script's Name
Python Spacing and Aligning Strings
How Do Threads Work in Python, and What Are Common Python-Threading Specific Pitfalls
How to Import a Module in Python with Importlib.Import_Module
Why How to Not Create a Wheel in Python
Can't Use '\1' Backreference to Capture-Group in a Function Call in Re.Sub() Repr Expression
Python Ignore Certificate Validation Urllib2
Why Use Sys.Path.Append(Path) Instead of Sys.Path.Insert(1, Path)
Pandas Latitude-Longitude to Distance Between Successive Rows
How to Read a Response from Python Requests
How to Align Gridlines for Two Y-Axis Scales Using Matplotlib