How to Hide Chrome Driver in Python

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)

Is there a way to hide the browser while running selenium in Python?

If you're using Chrome you can just set the headless argument like so:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

driver_exe = 'chromedriver'
options = Options()
options.add_argument("--headless")
driver = webdriver.Chrome(driver_exe, options=options)

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 Chrome Web Driver Icon (Python-Selenium)

Update (this worked for me to hide the chromedriver icons at least on my Mac):

I was not able to find a Chrome Option to accomplish hiding the chromedriver icons that appear in the dock. However, I was able to edit the Info.plist file for Chrome as part of my program to do the trick (of hiding chromedriver icons) using the LSBackgroundOnly key.

In the Info.plist for Chrome, programmatically, I entered:

<key>LSBackgroundOnly</key>
<string>1</string>

On my Mac using python3 code I used os.system to execute a terminal command using defaults to enter the LSBackgroundOnly key when the program is executing and then at the end of program execution I delete the LSBackgroundOnly key from the Info.plist.

like this:

1)

defaults write /Applications/Google\ Chrome.app/Contents/Info.plist LSBackgroundOnly -string '1'

2)

defaults delete /Applications/Google\ Chrome.app/Contents/Info.plist LSBackgroundOnly

AS INFO: This is tricky because your normal chrome app may start up in background mode if you don't add/delete LSBackgroundOnly properly during program execution. In my experience, worst case scenario you may have to manually remove LSBackgroundOnly from the Info.plist file then restart your computer to get Chrome out of background mode.

I remove the LSBackgroundOnly key from the Info.plist file in my program after program execution because the key only needs to be in the file when the chromedrivers are launched. After that I want to be able to use regular Chrome app without issues of it opening in background mode. But, if you understand your program execution and handle your exceptions, this will definitely work to hide the icons correctly and there will be nothing different in using your regular Chrome app.

Happy hacking.

How to hide Chromedriver console window?

You need to call driver.quit() at the end of the script:

quit()

Closes the browser and shuts down the ChromeDriver executable
that is started when starting the ChromeDriver

If you want to just close the service executable and let the browser stay opened, call:

driver.service.stop()

FYI, I've figured this out from the quit() method implementation (source code).



Related Topics



Leave a reply



Submit