Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added basic Firefox support #70

Merged
merged 1 commit into from
Nov 23, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Comment on lines +110 to +111
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason these lines are commented?


Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Found how to disable audio in Firefox as well, but couldn't find how to disable scrollbars or the other stuff that Chrome does (for now).

Suggested change
# Disable unused/annoying features
firefox_options.setPreference("media.volume_scale", "0.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