Set Chrome options from a single method

This commit is contained in:
Michael Mintz 2018-03-21 02:31:14 -04:00
parent 34fb23734d
commit 4b1933a43d
1 changed files with 43 additions and 66 deletions

View File

@ -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: