Add browser headless mode for Windows

This commit is contained in:
ngosang 2022-09-24 18:42:58 +02:00
parent 93041779fb
commit 3b9fd0aa6a
2 changed files with 12 additions and 3 deletions

View File

@ -119,6 +119,7 @@ class Chrome(selenium.webdriver.chrome.webdriver.WebDriver):
suppress_welcome=True,
use_subprocess=False,
debug=False,
windows_headless=False,
**kw
):
"""
@ -384,17 +385,21 @@ class Chrome(selenium.webdriver.chrome.webdriver.WebDriver):
if not desired_capabilities:
desired_capabilities = options.to_capabilities()
if not use_subprocess:
if not use_subprocess and not windows_headless:
self.browser_pid = start_detached(
options.binary_location, *options.arguments
)
else:
startupinfo = subprocess.STARTUPINFO()
if os.name == 'nt' and windows_headless:
startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
browser = subprocess.Popen(
[options.binary_location, *options.arguments],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
close_fds=IS_POSIX,
startupinfo=startupinfo
)
self.browser_pid = browser.pid

View File

@ -43,8 +43,11 @@ def get_webdriver() -> WebDriver:
# note: headless mode is detected (options.headless = True)
# we launch the browser in head-full mode with the window hidden
windows_headless = False
if get_config_headless():
if os.name != 'nt': # not in windows
if os.name == 'nt':
windows_headless = True
else:
start_xvfb_display()
# if we are inside the Docker container, we avoid downloading the driver
@ -57,7 +60,8 @@ def get_webdriver() -> WebDriver:
# downloads and patches the chromedriver
# todo: if we don't set driver_executable_path it downloads, patches, and deletes the driver each time
driver = uc.Chrome(options=options, driver_executable_path=driver_exe_path, version_main=version_main)
driver = uc.Chrome(options=options, driver_executable_path=driver_exe_path, version_main=version_main,
windows_headless=windows_headless)
# selenium vanilla
# options = webdriver.ChromeOptions()