selenium hide chromdriver console window

Question:

I have created a program with python where I am using selenium. I also created a .exe with pyinstaller. If I run the .exe chrome opens normally. But for some reason, the console appears too. This only happens if I am launching the .exe. If I launch the code in VS Code only the chrome window appears.

I found something that is written in C#:

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

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

My chromdriver setup:

option = webdriver.ChromeOptions()
option.add_argument("--incogneto")
chromedriver_path = driverPath
browser = webdriver.Chrome(chromedriver_path)
browser.set_window_size(1100, 800)
browser.get("myurl")

How can I do this in Python?

Asked By: adri567

||

Answers:

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!

Answered By: The Pilot Dude

Selenium4 is released with this feature! No need to edit library 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

# 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)

Update with pip install selenium OR upgrade the package pip install selenium --upgrade

Answered By: Ali Sajjad

Both answers here that change the creationflags field of the Service object don’t work anymore, since the name of that field was changed to the more pythonic name of creation_flags (see here and correspondingly here). So in newer versions like 4.6 the exemplary code to patch in the creation flags on Windows should look like:

from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService 
from subprocess import CREATE_NO_WINDOW

chrome_service = ChromeService('chromedriver')
chrome_service.creation_flags = CREATE_NO_WINDOW
driver = webdriver.Chrome(service=chrome_service)

…and analogously for Firefox drivers.

Answered By: hoppla1232