Skip to content

Commit

Permalink
Add profile param to start_firefox
Browse files Browse the repository at this point in the history
Thank you @a-l-e-c for the PR!
  • Loading branch information
a-l-e-c authored Nov 20, 2022
1 parent 1ac91d8 commit e9f181d
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
23 changes: 21 additions & 2 deletions helium/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,16 @@ def start_chrome(
url, headless, maximize, options, capabilities
)

def start_firefox(url=None, headless=False, options=None):
def start_firefox(url=None, headless=False, options=None, profile=None):
"""
:param url: URL to open.
:type url: str
:param headless: Whether to start Firefox in headless mode.
:type headless: bool
:param options: FirefoxOptions to use for starting the browser.
:type options: :py:class:`selenium.webdriver.FirefoxOptions`
:param profile: FirefoxProfile to use for starting the browser.
:type profile: :py:class:`selenium.webdriver.FirefoxProfile`
Starts an instance of Firefox::
Expand Down Expand Up @@ -112,14 +114,31 @@ def start_firefox(url=None, headless=False, options=None):
options.add_argument("--height=1440")
start_firefox(options=options)
To set proxy, useragent, etc. (ie. things you find in about:config), use the `profile` parameter::
from selenium.webdriver import FirefoxProfile
profile = FirefoxProfile()
SOCKS5_PROXY_HOST = "0.0.0.0"
PROXY_PORT = 0
profile.set_preference("network.proxy.type", 1)
profile.set_preference("network.proxy.socks", SOCKS5_PROXY_HOST)
profile.set_preference("network.proxy.socks_port", PROXY_PORT)
profile.set_preference("network.proxy.socks_remote_dns", True)
profile.set_preference("network.proxy.socks_version", 5)
profile.set_preference("network.proxy.no_proxies_on", "localhost, 10.20.30.40")
USER_AGENT = "Mozilla/5.0 ..."
profile.set_preference("general.useragent.override", USER_AGENT)
start_firefox(profile=profile)
On shutdown of the Python interpreter, Helium cleans up all resources used
for controlling the browser (such as the geckodriver process), but does
not close the browser itself. If you want to terminate the browser at the
end of your script, use the following command::
kill_browser()
"""
return _get_api_impl().start_firefox_impl(url, headless, options)
return _get_api_impl().start_firefox_impl(url, headless, options, profile)

def go_to(url):
"""
Expand Down
10 changes: 6 additions & 4 deletions helium/_impl/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from selenium.webdriver.remote.webelement import WebElement
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support.ui import Select
from selenium.webdriver import Chrome, ChromeOptions, Firefox, FirefoxOptions
from selenium.webdriver import Chrome, ChromeOptions, Firefox, FirefoxOptions, FirefoxProfile
from time import sleep, time

import atexit
Expand Down Expand Up @@ -74,15 +74,17 @@ class APIImpl:
" * set_driver(...)"
def __init__(self):
self.driver = None
def start_firefox_impl(self, url=None, headless=False, options=None):
firefox_driver = self._start_firefox_driver(headless, options)
def start_firefox_impl(self, url=None, headless=False, options=None, profile=None):
firefox_driver = self._start_firefox_driver(headless, options, profile)
return self._start(firefox_driver, url)
def _start_firefox_driver(self, headless, options):
def _start_firefox_driver(self, headless, options, profile):
firefox_options = FirefoxOptions() if options is None else options
firefox_profile = FirefoxProfile() if profile is None else profile
if headless:
firefox_options.headless = True
kwargs = {
'options': firefox_options,
'firefox_profile': firefox_profile,
'service_log_path': 'nul' if is_windows() else '/dev/null'
}
try:
Expand Down

0 comments on commit e9f181d

Please sign in to comment.