From 4b1933a43d1349c403df7a8aca85c81716fd64e2 Mon Sep 17 00:00:00 2001 From: Michael Mintz Date: Wed, 21 Mar 2018 02:31:14 -0400 Subject: [PATCH] Set Chrome options from a single method --- seleniumbase/core/browser_launcher.py | 109 ++++++++++---------------- 1 file changed, 43 insertions(+), 66 deletions(-) diff --git a/seleniumbase/core/browser_launcher.py b/seleniumbase/core/browser_launcher.py index 6917e05b..174ab4ff 100755 --- a/seleniumbase/core/browser_launcher.py +++ b/seleniumbase/core/browser_launcher.py @@ -9,6 +9,37 @@ from seleniumbase.core import download_helper from seleniumbase.fixtures import constants +def _set_chrome_options(downloads_path, proxy_string): + chrome_options = webdriver.ChromeOptions() + prefs = { + "download.default_directory": downloads_path, + "credentials_enable_service": False, + "profile": { + "password_manager_enabled": False + } + } + chrome_options.add_experimental_option("prefs", prefs) + chrome_options.add_argument("--test-type") + chrome_options.add_argument("--no-first-run") + chrome_options.add_argument("--ignore-certificate-errors") + chrome_options.add_argument("--allow-file-access-from-files") + chrome_options.add_argument("--allow-insecure-localhost") + chrome_options.add_argument("--allow-running-insecure-content") + chrome_options.add_argument("--disable-infobars") + chrome_options.add_argument("--disable-save-password-bubble") + chrome_options.add_argument("--disable-single-click-autofill") + chrome_options.add_argument("--disable-translate") + chrome_options.add_argument("--disable-web-security") + if proxy_string: + chrome_options.add_argument('--proxy-server=%s' % proxy_string) + if settings.START_CHROME_IN_FULL_SCREEN_MODE: + # Run Chrome in full screen mode on WINDOWS + chrome_options.add_argument("--start-maximized") + # Run Chrome in full screen mode on MAC/Linux + chrome_options.add_argument("--kiosk") + return chrome_options + + def _create_firefox_profile(downloads_path, proxy_string): profile = webdriver.FirefoxProfile() profile.set_preference("reader.parse-on-load.enabled", False) @@ -82,41 +113,14 @@ def get_remote_driver(browser_name, headless, servername, port, proxy_string): address = "http://%s:%s/wd/hub" % (servername, port) if browser_name == constants.Browser.GOOGLE_CHROME: - chrome_options = webdriver.ChromeOptions() - prefs = { - "download.default_directory": downloads_path, - "credentials_enable_service": False, - "profile": { - "password_manager_enabled": False - } - } - chrome_options.add_experimental_option("prefs", prefs) - chrome_options.add_argument("--test-type") - chrome_options.add_argument("--no-first-run") - chrome_options.add_argument("--ignore-certificate-errors") - chrome_options.add_argument("--allow-file-access-from-files") - chrome_options.add_argument("--allow-insecure-localhost") - chrome_options.add_argument("--allow-running-insecure-content") - chrome_options.add_argument("--disable-infobars") - chrome_options.add_argument("--disable-save-password-bubble") - chrome_options.add_argument("--disable-single-click-autofill") - chrome_options.add_argument("--disable-translate") - chrome_options.add_argument("--disable-web-security") + chrome_options = _set_chrome_options(downloads_path, proxy_string) if headless: chrome_options.add_argument("--headless") - if proxy_string: - chrome_options.add_argument('--proxy-server=%s' % proxy_string) - if settings.START_CHROME_IN_FULL_SCREEN_MODE: - # Run Chrome in full screen mode on WINDOWS - chrome_options.add_argument("--start-maximized") - # Run Chrome in full screen mode on MAC/Linux - chrome_options.add_argument("--kiosk") capabilities = chrome_options.to_capabilities() return webdriver.Remote( command_executor=address, desired_capabilities=capabilities) - - if browser_name == constants.Browser.FIREFOX: + elif browser_name == constants.Browser.FIREFOX: try: # Use Geckodriver for Firefox if it's on the PATH profile = _create_firefox_profile(downloads_path, proxy_string) @@ -144,23 +148,22 @@ def get_remote_driver(browser_name, headless, servername, port, proxy_string): command_executor=address, desired_capabilities=capabilities, browser_profile=profile) - - if browser_name == constants.Browser.INTERNET_EXPLORER: + elif browser_name == constants.Browser.INTERNET_EXPLORER: return webdriver.Remote( command_executor=address, desired_capabilities=( webdriver.DesiredCapabilities.INTERNETEXPLORER)) - if browser_name == constants.Browser.EDGE: + elif browser_name == constants.Browser.EDGE: return webdriver.Remote( command_executor=address, desired_capabilities=( webdriver.DesiredCapabilities.EDGE)) - if browser_name == constants.Browser.SAFARI: + elif browser_name == constants.Browser.SAFARI: return webdriver.Remote( command_executor=address, desired_capabilities=( webdriver.DesiredCapabilities.SAFARI)) - if browser_name == constants.Browser.PHANTOM_JS: + elif browser_name == constants.Browser.PHANTOM_JS: with warnings.catch_warnings(): # Ignore "PhantomJS has been deprecated" UserWarning warnings.simplefilter("ignore", category=UserWarning) @@ -203,48 +206,22 @@ def get_local_driver(browser_name, headless, proxy_string): if headless: raise Exception(e) return webdriver.Firefox() - if browser_name == constants.Browser.INTERNET_EXPLORER: + elif browser_name == constants.Browser.INTERNET_EXPLORER: return webdriver.Ie() - if browser_name == constants.Browser.EDGE: + elif browser_name == constants.Browser.EDGE: return webdriver.Edge() - if browser_name == constants.Browser.SAFARI: + elif browser_name == constants.Browser.SAFARI: return webdriver.Safari() - if browser_name == constants.Browser.PHANTOM_JS: + elif browser_name == constants.Browser.PHANTOM_JS: with warnings.catch_warnings(): # Ignore "PhantomJS has been deprecated" UserWarning warnings.simplefilter("ignore", category=UserWarning) return webdriver.PhantomJS() - if browser_name == constants.Browser.GOOGLE_CHROME: + elif browser_name == constants.Browser.GOOGLE_CHROME: try: - chrome_options = webdriver.ChromeOptions() - prefs = { - "download.default_directory": downloads_path, - "credentials_enable_service": False, - "profile": { - "password_manager_enabled": False - } - } - chrome_options.add_experimental_option("prefs", prefs) - chrome_options.add_argument("--test-type") - chrome_options.add_argument("--no-first-run") - chrome_options.add_argument("--ignore-certificate-errors") - chrome_options.add_argument("--allow-file-access-from-files") - chrome_options.add_argument("--allow-insecure-localhost") - chrome_options.add_argument("--allow-running-insecure-content") - chrome_options.add_argument("--disable-infobars") - chrome_options.add_argument("--disable-save-password-bubble") - chrome_options.add_argument("--disable-single-click-autofill") - chrome_options.add_argument("--disable-translate") - chrome_options.add_argument("--disable-web-security") + chrome_options = _set_chrome_options(downloads_path, proxy_string) if headless: chrome_options.add_argument("--headless") - if proxy_string: - chrome_options.add_argument('--proxy-server=%s' % proxy_string) - if settings.START_CHROME_IN_FULL_SCREEN_MODE: - # Run Chrome in full screen mode on WINDOWS - chrome_options.add_argument("--start-maximized") - # Run Chrome in full screen mode on MAC/Linux - chrome_options.add_argument("--kiosk") return webdriver.Chrome(options=chrome_options) except Exception as e: if headless: