Skip to content

Commit 4b1933a

Browse files
committed
Set Chrome options from a single method
1 parent 34fb237 commit 4b1933a

File tree

1 file changed

+43
-66
lines changed

1 file changed

+43
-66
lines changed

seleniumbase/core/browser_launcher.py

Lines changed: 43 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,37 @@
99
from seleniumbase.fixtures import constants
1010

1111

12+
def _set_chrome_options(downloads_path, proxy_string):
13+
chrome_options = webdriver.ChromeOptions()
14+
prefs = {
15+
"download.default_directory": downloads_path,
16+
"credentials_enable_service": False,
17+
"profile": {
18+
"password_manager_enabled": False
19+
}
20+
}
21+
chrome_options.add_experimental_option("prefs", prefs)
22+
chrome_options.add_argument("--test-type")
23+
chrome_options.add_argument("--no-first-run")
24+
chrome_options.add_argument("--ignore-certificate-errors")
25+
chrome_options.add_argument("--allow-file-access-from-files")
26+
chrome_options.add_argument("--allow-insecure-localhost")
27+
chrome_options.add_argument("--allow-running-insecure-content")
28+
chrome_options.add_argument("--disable-infobars")
29+
chrome_options.add_argument("--disable-save-password-bubble")
30+
chrome_options.add_argument("--disable-single-click-autofill")
31+
chrome_options.add_argument("--disable-translate")
32+
chrome_options.add_argument("--disable-web-security")
33+
if proxy_string:
34+
chrome_options.add_argument('--proxy-server=%s' % proxy_string)
35+
if settings.START_CHROME_IN_FULL_SCREEN_MODE:
36+
# Run Chrome in full screen mode on WINDOWS
37+
chrome_options.add_argument("--start-maximized")
38+
# Run Chrome in full screen mode on MAC/Linux
39+
chrome_options.add_argument("--kiosk")
40+
return chrome_options
41+
42+
1243
def _create_firefox_profile(downloads_path, proxy_string):
1344
profile = webdriver.FirefoxProfile()
1445
profile.set_preference("reader.parse-on-load.enabled", False)
@@ -82,41 +113,14 @@ def get_remote_driver(browser_name, headless, servername, port, proxy_string):
82113
address = "http://%s:%s/wd/hub" % (servername, port)
83114

84115
if browser_name == constants.Browser.GOOGLE_CHROME:
85-
chrome_options = webdriver.ChromeOptions()
86-
prefs = {
87-
"download.default_directory": downloads_path,
88-
"credentials_enable_service": False,
89-
"profile": {
90-
"password_manager_enabled": False
91-
}
92-
}
93-
chrome_options.add_experimental_option("prefs", prefs)
94-
chrome_options.add_argument("--test-type")
95-
chrome_options.add_argument("--no-first-run")
96-
chrome_options.add_argument("--ignore-certificate-errors")
97-
chrome_options.add_argument("--allow-file-access-from-files")
98-
chrome_options.add_argument("--allow-insecure-localhost")
99-
chrome_options.add_argument("--allow-running-insecure-content")
100-
chrome_options.add_argument("--disable-infobars")
101-
chrome_options.add_argument("--disable-save-password-bubble")
102-
chrome_options.add_argument("--disable-single-click-autofill")
103-
chrome_options.add_argument("--disable-translate")
104-
chrome_options.add_argument("--disable-web-security")
116+
chrome_options = _set_chrome_options(downloads_path, proxy_string)
105117
if headless:
106118
chrome_options.add_argument("--headless")
107-
if proxy_string:
108-
chrome_options.add_argument('--proxy-server=%s' % proxy_string)
109-
if settings.START_CHROME_IN_FULL_SCREEN_MODE:
110-
# Run Chrome in full screen mode on WINDOWS
111-
chrome_options.add_argument("--start-maximized")
112-
# Run Chrome in full screen mode on MAC/Linux
113-
chrome_options.add_argument("--kiosk")
114119
capabilities = chrome_options.to_capabilities()
115120
return webdriver.Remote(
116121
command_executor=address,
117122
desired_capabilities=capabilities)
118-
119-
if browser_name == constants.Browser.FIREFOX:
123+
elif browser_name == constants.Browser.FIREFOX:
120124
try:
121125
# Use Geckodriver for Firefox if it's on the PATH
122126
profile = _create_firefox_profile(downloads_path, proxy_string)
@@ -144,23 +148,22 @@ def get_remote_driver(browser_name, headless, servername, port, proxy_string):
144148
command_executor=address,
145149
desired_capabilities=capabilities,
146150
browser_profile=profile)
147-
148-
if browser_name == constants.Browser.INTERNET_EXPLORER:
151+
elif browser_name == constants.Browser.INTERNET_EXPLORER:
149152
return webdriver.Remote(
150153
command_executor=address,
151154
desired_capabilities=(
152155
webdriver.DesiredCapabilities.INTERNETEXPLORER))
153-
if browser_name == constants.Browser.EDGE:
156+
elif browser_name == constants.Browser.EDGE:
154157
return webdriver.Remote(
155158
command_executor=address,
156159
desired_capabilities=(
157160
webdriver.DesiredCapabilities.EDGE))
158-
if browser_name == constants.Browser.SAFARI:
161+
elif browser_name == constants.Browser.SAFARI:
159162
return webdriver.Remote(
160163
command_executor=address,
161164
desired_capabilities=(
162165
webdriver.DesiredCapabilities.SAFARI))
163-
if browser_name == constants.Browser.PHANTOM_JS:
166+
elif browser_name == constants.Browser.PHANTOM_JS:
164167
with warnings.catch_warnings():
165168
# Ignore "PhantomJS has been deprecated" UserWarning
166169
warnings.simplefilter("ignore", category=UserWarning)
@@ -203,48 +206,22 @@ def get_local_driver(browser_name, headless, proxy_string):
203206
if headless:
204207
raise Exception(e)
205208
return webdriver.Firefox()
206-
if browser_name == constants.Browser.INTERNET_EXPLORER:
209+
elif browser_name == constants.Browser.INTERNET_EXPLORER:
207210
return webdriver.Ie()
208-
if browser_name == constants.Browser.EDGE:
211+
elif browser_name == constants.Browser.EDGE:
209212
return webdriver.Edge()
210-
if browser_name == constants.Browser.SAFARI:
213+
elif browser_name == constants.Browser.SAFARI:
211214
return webdriver.Safari()
212-
if browser_name == constants.Browser.PHANTOM_JS:
215+
elif browser_name == constants.Browser.PHANTOM_JS:
213216
with warnings.catch_warnings():
214217
# Ignore "PhantomJS has been deprecated" UserWarning
215218
warnings.simplefilter("ignore", category=UserWarning)
216219
return webdriver.PhantomJS()
217-
if browser_name == constants.Browser.GOOGLE_CHROME:
220+
elif browser_name == constants.Browser.GOOGLE_CHROME:
218221
try:
219-
chrome_options = webdriver.ChromeOptions()
220-
prefs = {
221-
"download.default_directory": downloads_path,
222-
"credentials_enable_service": False,
223-
"profile": {
224-
"password_manager_enabled": False
225-
}
226-
}
227-
chrome_options.add_experimental_option("prefs", prefs)
228-
chrome_options.add_argument("--test-type")
229-
chrome_options.add_argument("--no-first-run")
230-
chrome_options.add_argument("--ignore-certificate-errors")
231-
chrome_options.add_argument("--allow-file-access-from-files")
232-
chrome_options.add_argument("--allow-insecure-localhost")
233-
chrome_options.add_argument("--allow-running-insecure-content")
234-
chrome_options.add_argument("--disable-infobars")
235-
chrome_options.add_argument("--disable-save-password-bubble")
236-
chrome_options.add_argument("--disable-single-click-autofill")
237-
chrome_options.add_argument("--disable-translate")
238-
chrome_options.add_argument("--disable-web-security")
222+
chrome_options = _set_chrome_options(downloads_path, proxy_string)
239223
if headless:
240224
chrome_options.add_argument("--headless")
241-
if proxy_string:
242-
chrome_options.add_argument('--proxy-server=%s' % proxy_string)
243-
if settings.START_CHROME_IN_FULL_SCREEN_MODE:
244-
# Run Chrome in full screen mode on WINDOWS
245-
chrome_options.add_argument("--start-maximized")
246-
# Run Chrome in full screen mode on MAC/Linux
247-
chrome_options.add_argument("--kiosk")
248225
return webdriver.Chrome(options=chrome_options)
249226
except Exception as e:
250227
if headless:

0 commit comments

Comments
 (0)