Skip to content

Commit

Permalink
Added basic Firefox support
Browse files Browse the repository at this point in the history
  • Loading branch information
naslundx committed Nov 16, 2020
1 parent 1d25a80 commit 979d4b2
Showing 1 changed file with 39 additions and 7 deletions.
46 changes: 39 additions & 7 deletions webtraversallibrary/webdrivers.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,37 @@ def _add_script(driver: WebDriver, script_path: str):
return driver


def _setup_firefox(config: Config, profile_path: Path = None) -> WebDriver:
firefox_profile = webdriver.FirefoxProfile()
firefox_options = webdriver.FirefoxOptions()
browser = config.browser

# Disable security
firefox_options.add_argument("-safe-mode")

# Force opening in new tabs
# firefox_options.set_preference("browser.link.open_newwindow", 3)
# firefox_options.set_preference("browser.link.open_newwindow.restriction", 0)

firefox_options.headless = browser.headless

if browser.proxy:
address = browser.proxy.split(":")
firefox_profile.set_preference("network.proxy.http", address[0])
firefox_profile.set_preference("network.proxy.http_port", address[1])

if "useragent" in browser:
firefox_profile.set_preference("general.useragent.override", browser.useragent)

driver = webdriver.Firefox(options=firefox_options, firefox_profile=firefox_profile)

# Screen size (we can't set viewport size as for Chrome so we adjust screen size)
if "width" in browser and "height" in browser:
driver.set_window_size(browser.width, browser.height)

return driver


def setup_driver(config: Config, profile_path: Path = None, preload_callbacks: List[Path] = None) -> WebDriver:
"""
Creates a WebDriver object with the given configuration.
Expand All @@ -108,14 +139,15 @@ def setup_driver(config: Config, profile_path: Path = None, preload_callbacks: L

if config.browser.browser == "chrome":
driver = _setup_chrome(config=config, profile_path=profile_path)
elif config.browser.browser == "firefox":
driver = _setup_firefox(config=config, profile_path=profile_path)
else:
raise NotImplementedError(f"Uninmplemented browser type given to setup_driver: {config.browser.browser}")

if driver:
preload_callbacks = preload_callbacks or []
for cb in preload_callbacks:
driver.add_script(str(cb))
return driver

raise NotImplementedError(f"Uninmplemented browser type given to setup_driver: {config.browser}")
preload_callbacks = preload_callbacks or []
for cb in preload_callbacks:
driver.add_script(str(cb))
return driver


def send(driver: WebDriver, cmd: str, params: dict = None) -> int:
Expand Down

0 comments on commit 979d4b2

Please sign in to comment.