Skip to content

Commit 566865a

Browse files
committed
Refactor browser_launcher.py
1 parent 879e6a1 commit 566865a

File tree

1 file changed

+102
-147
lines changed

1 file changed

+102
-147
lines changed

seleniumbase/core/browser_launcher.py

Lines changed: 102 additions & 147 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,16 @@
1-
import json
21
import logging
32
import os
4-
import random
53
import re
64
import sys
75
import time
86
import urllib3
97
import warnings
108
from selenium import webdriver
11-
from selenium.common.exceptions import WebDriverException
12-
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
139
from seleniumbase.config import proxy_list
1410
from seleniumbase.config import settings
1511
from seleniumbase.core import download_helper
1612
from seleniumbase.core import proxy_helper
1713
from seleniumbase.fixtures import constants
18-
from seleniumbase.fixtures import page_utils
1914
from seleniumbase import drivers # webdriver storage folder for SeleniumBase
2015
from seleniumbase import extensions # browser extensions storage folder
2116
urllib3.disable_warnings()
@@ -98,6 +93,7 @@ def _add_chrome_proxy_extension(
9893
""" Implementation of https://stackoverflow.com/a/35293284 for
9994
https://stackoverflow.com/questions/12848327/
10095
(Run Selenium on a proxy server that requires authentication.) """
96+
import random
10197
arg_join = " ".join(sys.argv)
10298
if not ("-n" in sys.argv or "-n=" in arg_join or arg_join == "-c"):
10399
# Single-threaded
@@ -267,7 +263,7 @@ def _set_chrome_options(
267263
if proxy_auth:
268264
chrome_options = _add_chrome_proxy_extension(
269265
chrome_options, proxy_string, proxy_user, proxy_pass)
270-
chrome_options.add_argument('--proxy-server=%s' % proxy_string)
266+
chrome_options.add_argument("--proxy-server=%s" % proxy_string)
271267
if headless:
272268
if not proxy_auth and not browser_name == constants.Browser.OPERA:
273269
# Headless Chrome doesn't support extensions, which are
@@ -291,7 +287,7 @@ def _set_chrome_options(
291287
# To access the Remote Debugger, go to: http://localhost:9222
292288
# while a Chromium driver is running.
293289
# Info: https://chromedevtools.github.io/devtools-protocol/
294-
chrome_options.add_argument('--remote-debugging-port=9222')
290+
chrome_options.add_argument("--remote-debugging-port=9222")
295291
if swiftshader:
296292
chrome_options.add_argument("--use-gl=swiftshader")
297293
else:
@@ -320,37 +316,38 @@ def _set_safari_capabilities():
320316
return safari_capabilities
321317

322318

323-
def _create_firefox_profile(
324-
downloads_path, locale_code, proxy_string, user_agent, disable_csp):
325-
profile = webdriver.FirefoxProfile()
326-
profile.accept_untrusted_certs = True
327-
profile.set_preference("reader.parse-on-load.enabled", False)
328-
profile.set_preference("pdfjs.disabled", True)
329-
profile.set_preference("app.update.auto", False)
330-
profile.set_preference("app.update.enabled", False)
331-
profile.set_preference("app.update.silent", True)
332-
profile.set_preference("browser.formfill.enable", False)
333-
profile.set_preference("browser.privatebrowsing.autostart", True)
334-
profile.set_preference("devtools.errorconsole.enabled", True)
335-
profile.set_preference("dom.webnotifications.enabled", False)
336-
profile.set_preference("dom.disable_beforeunload", True)
337-
profile.set_preference("browser.contentblocking.database.enabled", False)
338-
profile.set_preference("extensions.allowPrivateBrowsingByDefault", True)
339-
profile.set_preference("extensions.PrivateBrowsing.notification", False)
340-
profile.set_preference("extensions.systemAddon.update.enabled", False)
341-
profile.set_preference("extensions.update.autoUpdateDefault", False)
342-
profile.set_preference("extensions.update.enabled", False)
343-
profile.set_preference("extensions.update.silent", True)
344-
profile.set_preference(
319+
def _set_firefox_options(
320+
downloads_path, headless, locale_code,
321+
proxy_string, user_agent, disable_csp):
322+
options = webdriver.FirefoxOptions()
323+
options.accept_untrusted_certs = True
324+
options.set_preference("reader.parse-on-load.enabled", False)
325+
options.set_preference("pdfjs.disabled", True)
326+
options.set_preference("app.update.auto", False)
327+
options.set_preference("app.update.enabled", False)
328+
options.set_preference("app.update.silent", True)
329+
options.set_preference("browser.formfill.enable", False)
330+
options.set_preference("browser.privatebrowsing.autostart", True)
331+
options.set_preference("devtools.errorconsole.enabled", True)
332+
options.set_preference("dom.webnotifications.enabled", False)
333+
options.set_preference("dom.disable_beforeunload", True)
334+
options.set_preference("browser.contentblocking.database.enabled", False)
335+
options.set_preference("extensions.allowPrivateBrowsingByDefault", True)
336+
options.set_preference("extensions.PrivateBrowsing.notification", False)
337+
options.set_preference("extensions.systemAddon.update.enabled", False)
338+
options.set_preference("extensions.update.autoUpdateDefault", False)
339+
options.set_preference("extensions.update.enabled", False)
340+
options.set_preference("extensions.update.silent", True)
341+
options.set_preference(
345342
"datareporting.healthreport.logging.consoleEnabled", False)
346-
profile.set_preference("datareporting.healthreport.service.enabled", False)
347-
profile.set_preference(
343+
options.set_preference("datareporting.healthreport.service.enabled", False)
344+
options.set_preference(
348345
"datareporting.healthreport.service.firstRun", False)
349-
profile.set_preference("datareporting.healthreport.uploadEnabled", False)
350-
profile.set_preference("datareporting.policy.dataSubmissionEnabled", False)
351-
profile.set_preference(
346+
options.set_preference("datareporting.healthreport.uploadEnabled", False)
347+
options.set_preference("datareporting.policy.dataSubmissionEnabled", False)
348+
options.set_preference(
352349
"datareporting.policy.dataSubmissionPolicyAccepted", False)
353-
profile.set_preference("toolkit.telemetry.unified", False)
350+
options.set_preference("toolkit.telemetry.unified", False)
354351
if proxy_string:
355352
socks_proxy = False
356353
socks_ver = 0
@@ -366,44 +363,46 @@ def _create_firefox_profile(
366363
else:
367364
proxy_server = proxy_string.split(':')[0]
368365
proxy_port = proxy_string.split(':')[1]
369-
profile.set_preference("network.proxy.type", 1)
366+
options.set_preference("network.proxy.type", 1)
370367
if socks_proxy:
371-
profile.set_preference('network.proxy.socks', proxy_server)
372-
profile.set_preference('network.proxy.socks_port', int(proxy_port))
373-
profile.set_preference('network.proxy.socks_version', socks_ver)
368+
options.set_preference('network.proxy.socks', proxy_server)
369+
options.set_preference('network.proxy.socks_port', int(proxy_port))
370+
options.set_preference('network.proxy.socks_version', socks_ver)
374371
else:
375-
profile.set_preference("network.proxy.http", proxy_server)
376-
profile.set_preference("network.proxy.http_port", int(proxy_port))
377-
profile.set_preference("network.proxy.ssl", proxy_server)
378-
profile.set_preference("network.proxy.ssl_port", int(proxy_port))
372+
options.set_preference("network.proxy.http", proxy_server)
373+
options.set_preference("network.proxy.http_port", int(proxy_port))
374+
options.set_preference("network.proxy.ssl", proxy_server)
375+
options.set_preference("network.proxy.ssl_port", int(proxy_port))
379376
if user_agent:
380-
profile.set_preference("general.useragent.override", user_agent)
381-
profile.set_preference(
377+
options.set_preference("general.useragent.override", user_agent)
378+
options.set_preference(
382379
"security.mixed_content.block_active_content", False)
383380
if settings.DISABLE_CSP_ON_FIREFOX or disable_csp:
384-
profile.set_preference("security.csp.enable", False)
385-
profile.set_preference(
381+
options.set_preference("security.csp.enable", False)
382+
options.set_preference(
386383
"browser.download.manager.showAlertOnComplete", False)
384+
if headless:
385+
options.add_argument("--headless")
387386
if locale_code:
388-
profile.set_preference("intl.accept_languages", locale_code)
389-
profile.set_preference("browser.shell.checkDefaultBrowser", False)
390-
profile.set_preference("browser.startup.page", 0)
391-
profile.set_preference("browser.download.panel.shown", False)
392-
profile.set_preference(
387+
options.set_preference("intl.accept_languages", locale_code)
388+
options.set_preference("browser.shell.checkDefaultBrowser", False)
389+
options.set_preference("browser.startup.page", 0)
390+
options.set_preference("browser.download.panel.shown", False)
391+
options.set_preference(
393392
"browser.download.animateNotifications", False)
394-
profile.set_preference("browser.download.dir", downloads_path)
395-
profile.set_preference("browser.download.folderList", 2)
396-
profile.set_preference("browser.helperApps.alwaysAsk.force", False)
397-
profile.set_preference(
393+
options.set_preference("browser.download.dir", downloads_path)
394+
options.set_preference("browser.download.folderList", 2)
395+
options.set_preference("browser.helperApps.alwaysAsk.force", False)
396+
options.set_preference(
398397
"browser.download.manager.showWhenStarting", False)
399-
profile.set_preference(
398+
options.set_preference(
400399
"browser.helperApps.neverAsk.saveToDisk",
401400
("application/pdf, application/zip, application/octet-stream, "
402401
"text/csv, text/xml, application/xml, text/plain, "
403402
"text/octet-stream, application/x-gzip, application/x-tar "
404403
"application/"
405404
"vnd.openxmlformats-officedocument.spreadsheetml.sheet"))
406-
return profile
405+
return options
407406

408407

409408
def display_proxy_warning(proxy_string):
@@ -422,6 +421,7 @@ def display_proxy_warning(proxy_string):
422421

423422

424423
def validate_proxy_string(proxy_string):
424+
from seleniumbase.fixtures import page_utils
425425
if proxy_string in proxy_list.PROXY_LIST.keys():
426426
proxy_string = proxy_list.PROXY_LIST[proxy_string]
427427
if not proxy_string:
@@ -464,7 +464,7 @@ def validate_proxy_string(proxy_string):
464464

465465

466466
def get_driver(browser_name, headless=False, locale_code=None,
467-
use_grid=False, servername='localhost', port=4444,
467+
use_grid=False, servername="localhost", port=4444,
468468
proxy_string=None, user_agent=None,
469469
cap_file=None, cap_string=None,
470470
disable_csp=None, enable_ws=None, enable_sync=None,
@@ -540,6 +540,7 @@ def get_remote_driver(
540540
from seleniumbase.core import capabilities_parser
541541
desired_caps = capabilities_parser.get_desired_capabilities(cap_file)
542542
if cap_string:
543+
import json
543544
try:
544545
extra_caps = json.loads(cap_string)
545546
except Exception as e:
@@ -568,52 +569,28 @@ def get_remote_driver(
568569
capabilities = chrome_options.to_capabilities()
569570
for key in desired_caps.keys():
570571
capabilities[key] = desired_caps[key]
572+
warnings.simplefilter("ignore", category=DeprecationWarning)
571573
return webdriver.Remote(
572574
command_executor=address,
573575
desired_capabilities=capabilities,
574576
keep_alive=True)
575577
elif browser_name == constants.Browser.FIREFOX:
576-
try:
577-
# Use Geckodriver for Firefox if it's on the PATH
578-
profile = _create_firefox_profile(
579-
downloads_path, locale_code,
580-
proxy_string, user_agent, disable_csp)
581-
firefox_capabilities = DesiredCapabilities.FIREFOX.copy()
582-
firefox_capabilities['marionette'] = True
583-
if headless:
584-
firefox_capabilities['moz:firefoxOptions'] = (
585-
{'args': ['-headless']})
586-
for key in desired_caps.keys():
587-
firefox_capabilities[key] = desired_caps[key]
588-
capabilities = firefox_capabilities
589-
warnings.simplefilter("ignore", category=DeprecationWarning)
590-
return webdriver.Remote(
591-
command_executor=address,
592-
desired_capabilities=capabilities,
593-
browser_profile=profile,
594-
keep_alive=True)
595-
except WebDriverException:
596-
# Don't use Geckodriver: Only works for old versions of Firefox
597-
profile = _create_firefox_profile(
598-
downloads_path, locale_code,
599-
proxy_string, user_agent, disable_csp)
600-
firefox_capabilities = DesiredCapabilities.FIREFOX.copy()
601-
firefox_capabilities['marionette'] = False
602-
if headless:
603-
firefox_capabilities['moz:firefoxOptions'] = (
604-
{'args': ['-headless']})
605-
for key in desired_caps.keys():
606-
firefox_capabilities[key] = desired_caps[key]
607-
capabilities = firefox_capabilities
608-
return webdriver.Remote(
609-
command_executor=address,
610-
desired_capabilities=capabilities,
611-
browser_profile=profile,
612-
keep_alive=True)
578+
firefox_options = _set_firefox_options(
579+
downloads_path, headless, locale_code,
580+
proxy_string, user_agent, disable_csp)
581+
capabilities = firefox_options.to_capabilities()
582+
for key in desired_caps.keys():
583+
capabilities[key] = desired_caps[key]
584+
warnings.simplefilter("ignore", category=DeprecationWarning)
585+
return webdriver.Remote(
586+
command_executor=address,
587+
desired_capabilities=capabilities,
588+
keep_alive=True)
613589
elif browser_name == constants.Browser.INTERNET_EXPLORER:
614590
capabilities = webdriver.DesiredCapabilities.INTERNETEXPLORER
615591
for key in desired_caps.keys():
616592
capabilities[key] = desired_caps[key]
593+
warnings.simplefilter("ignore", category=DeprecationWarning)
617594
return webdriver.Remote(
618595
command_executor=address,
619596
desired_capabilities=capabilities,
@@ -622,6 +599,7 @@ def get_remote_driver(
622599
capabilities = webdriver.DesiredCapabilities.EDGE
623600
for key in desired_caps.keys():
624601
capabilities[key] = desired_caps[key]
602+
warnings.simplefilter("ignore", category=DeprecationWarning)
625603
return webdriver.Remote(
626604
command_executor=address,
627605
desired_capabilities=capabilities,
@@ -630,6 +608,7 @@ def get_remote_driver(
630608
capabilities = webdriver.DesiredCapabilities.SAFARI
631609
for key in desired_caps.keys():
632610
capabilities[key] = desired_caps[key]
611+
warnings.simplefilter("ignore", category=DeprecationWarning)
633612
return webdriver.Remote(
634613
command_executor=address,
635614
desired_capabilities=capabilities,
@@ -638,6 +617,7 @@ def get_remote_driver(
638617
capabilities = webdriver.DesiredCapabilities.OPERA
639618
for key in desired_caps.keys():
640619
capabilities[key] = desired_caps[key]
620+
warnings.simplefilter("ignore", category=DeprecationWarning)
641621
return webdriver.Remote(
642622
command_executor=address,
643623
desired_capabilities=capabilities,
@@ -699,56 +679,31 @@ def get_local_driver(
699679
downloads_path = download_helper.get_downloads_folder()
700680

701681
if browser_name == constants.Browser.FIREFOX:
702-
try:
682+
firefox_options = _set_firefox_options(
683+
downloads_path, headless, locale_code,
684+
proxy_string, user_agent, disable_csp)
685+
if LOCAL_GECKODRIVER and os.path.exists(LOCAL_GECKODRIVER):
703686
try:
704-
# Use Geckodriver for Firefox if it's on the PATH
705-
profile = _create_firefox_profile(
706-
downloads_path, locale_code,
707-
proxy_string, user_agent, disable_csp)
708-
firefox_capabilities = DesiredCapabilities.FIREFOX.copy()
709-
firefox_capabilities['marionette'] = True
710-
options = webdriver.FirefoxOptions()
711-
if headless:
712-
options.add_argument('-headless')
713-
firefox_capabilities['moz:firefoxOptions'] = (
714-
{'args': ['-headless']})
715-
if LOCAL_GECKODRIVER and os.path.exists(LOCAL_GECKODRIVER):
716-
try:
717-
make_driver_executable_if_not(LOCAL_GECKODRIVER)
718-
except Exception as e:
719-
logging.debug("\nWarning: Could not make geckodriver"
720-
" executable: %s" % e)
721-
elif not is_geckodriver_on_path():
722-
args = " ".join(sys.argv)
723-
if not ("-n" in sys.argv or "-n=" in args or args == "-c"):
724-
# (Not multithreaded)
725-
from seleniumbase.console_scripts import sb_install
726-
sys_args = sys.argv # Save a copy of current sys args
727-
print("\nWarning: geckodriver not found!"
728-
" Installing now:")
729-
try:
730-
sb_install.main(override="geckodriver")
731-
except Exception as e:
732-
print("\nWarning: Could not install geckodriver: "
733-
"%s" % e)
734-
sys.argv = sys_args # Put back the original sys args
735-
firefox_driver = webdriver.Firefox(
736-
firefox_profile=profile,
737-
capabilities=firefox_capabilities,
738-
options=options)
739-
except Exception:
740-
profile = _create_firefox_profile(
741-
downloads_path, locale_code,
742-
proxy_string, user_agent, disable_csp)
743-
firefox_capabilities = DesiredCapabilities.FIREFOX.copy()
744-
firefox_driver = webdriver.Firefox(
745-
firefox_profile=profile,
746-
capabilities=firefox_capabilities)
747-
return firefox_driver
748-
except Exception as e:
749-
if headless:
750-
raise Exception(e)
751-
return webdriver.Firefox()
687+
make_driver_executable_if_not(LOCAL_GECKODRIVER)
688+
except Exception as e:
689+
logging.debug("\nWarning: Could not make geckodriver"
690+
" executable: %s" % e)
691+
elif not is_geckodriver_on_path():
692+
args = " ".join(sys.argv)
693+
if not ("-n" in sys.argv or "-n=" in args or args == "-c"):
694+
# (Not multithreaded)
695+
from seleniumbase.console_scripts import sb_install
696+
sys_args = sys.argv # Save a copy of current sys args
697+
print("\nWarning: geckodriver not found!"
698+
" Installing now:")
699+
try:
700+
sb_install.main(override="geckodriver")
701+
except Exception as e:
702+
print("\nWarning: Could not install geckodriver: "
703+
"%s" % e)
704+
sys.argv = sys_args # Put back the original sys args
705+
warnings.simplefilter("ignore", category=DeprecationWarning)
706+
return webdriver.Firefox(options=firefox_options)
752707
elif browser_name == constants.Browser.INTERNET_EXPLORER:
753708
if not IS_WINDOWS:
754709
raise Exception(
@@ -898,7 +853,7 @@ def get_local_driver(
898853
if proxy_auth:
899854
edge_options = _add_chrome_proxy_extension(
900855
edge_options, proxy_string, proxy_user, proxy_pass)
901-
edge_options.add_argument('--proxy-server=%s' % proxy_string)
856+
edge_options.add_argument("--proxy-server=%s" % proxy_string)
902857
edge_options.add_argument("--test-type")
903858
edge_options.add_argument("--log-level=3")
904859
edge_options.add_argument("--no-first-run")
@@ -915,7 +870,7 @@ def get_local_driver(
915870
# To access the Remote Debugger, go to: http://localhost:9222
916871
# while a Chromium driver is running.
917872
# Info: https://chromedevtools.github.io/devtools-protocol/
918-
edge_options.add_argument('--remote-debugging-port=9222')
873+
edge_options.add_argument("--remote-debugging-port=9222")
919874
if swiftshader:
920875
edge_options.add_argument("--use-gl=swiftshader")
921876
else:

0 commit comments

Comments
 (0)