Skip to content

Commit

Permalink
Switch to Selenium Manager for webdrivers
Browse files Browse the repository at this point in the history
This commit introduces a backwards-incompatible API change:
start_chrome(...) no longer accepts a `capabilities` parameter.
  • Loading branch information
mherrmann committed Dec 8, 2023
1 parent fe10d38 commit 6dcbbc9
Show file tree
Hide file tree
Showing 7 changed files with 13 additions and 91 deletions.
3 changes: 0 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,6 @@ So in other words, you don't lose anything by using Helium over pure Selenium.
In addition to its more high-level API, Helium simplifies further tasks that are
traditionally painful in Selenium:

- **Web driver management:** Helium ships with its own copies of geckodriver and
automatically downloads a matching ChromeDriver for your system, so you don't
need to manually download and put them on your PATH.
- **iFrames:** Unlike Selenium, Helium lets you interact with elements inside
nested iFrames, without having to first "switch to" the iFrame.
- **Window management.** Helium notices when popups open or close and focuses /
Expand Down
30 changes: 7 additions & 23 deletions helium/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@

import helium._impl

def start_chrome(
url=None, headless=False, maximize=False, options=None, capabilities=None
):
def start_chrome(url=None, headless=False, maximize=False, options=None):
"""
:param url: URL to open.
:type url: str
Expand All @@ -31,8 +29,6 @@ def start_chrome(
:type maximize: bool
:param options: ChromeOptions to use for starting the browser
:type options: :py:class:`selenium.webdriver.ChromeOptions`
:param capabilities: DesiredCapabilities to use for starting the browser
:type capabilities: :py:class:`selenium.webdriver.DesiredCapabilities`
Starts an instance of Google Chrome::
Expand All @@ -48,36 +44,24 @@ def start_chrome(
start_chrome(headless=True)
start_chrome("google.com", headless=True)
For more advanced configuration, use the `options` or `capabilities`
parameters::
For more advanced configuration, use the `options` parameter::
from selenium.webdriver import ChromeOptions
options = ChromeOptions()
options.add_argument('--proxy-server=1.2.3.4:5678')
options.set_capability('goog:loggingPrefs', {'performance': 'ALL'})
start_chrome(options=options)
from selenium.webdriver import DesiredCapabilities
capabilities = DesiredCapabilities.CHROME
capabilities["pageLoadStrategy"] = "none"
capabilities["goog:loggingPrefs"] = {'performance': 'ALL'}
start_chrome(capabilities=capabilities)
When no compatible ChromeDriver is found on your `PATH`, then `start_chrome`
automatically downloads it into the following directories::
* Windows: `%LOCALAPPDATA%\Cache\Helium`
* macOS: `~/Library/Caches/Helium`
* Linux: `$XDG_CACHE_HOME/helium` or `~/.cache/Helium`
automatically downloads it using Selenium Manager.
On shutdown of the Python interpreter, Helium terminates the ChromeDriver
process but does not close the browser itself. If you want to close the
browser at the end of your script, use the following command::
kill_browser()
"""
return _get_api_impl().start_chrome_impl(
url, headless, maximize, options, capabilities
)
return _get_api_impl().start_chrome_impl(url, headless, maximize, options)

def start_firefox(url=None, headless=False, options=None, profile=None):
"""
Expand Down Expand Up @@ -116,7 +100,8 @@ def start_firefox(url=None, headless=False, options=None, profile=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::
To set proxy, useragent, etc. (ie. things you find in about:config), use the
`profile` parameter::
from selenium.webdriver import FirefoxProfile
profile = FirefoxProfile()
Expand All @@ -132,7 +117,6 @@ def start_firefox(url=None, headless=False, options=None, profile=None):
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
Expand Down
22 changes: 4 additions & 18 deletions helium/_impl/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from copy import copy
from helium._impl.chromedriver import install_matching_chromedriver
from helium._impl.match_type import PREFIX_IGNORE_CASE
from helium._impl.selenium_wrappers import WebElementWrapper, \
WebDriverWrapper, FrameIterator, FramesChangedWhileIterating
Expand All @@ -11,7 +10,6 @@
ElementNotVisibleException, MoveTargetOutOfBoundsException, \
WebDriverException, StaleElementReferenceException, \
NoAlertPresentException, NoSuchWindowException
from selenium.webdriver.chrome.service import Service as ServiceChrome
from selenium.webdriver.common.by import By
from selenium.webdriver.firefox.service import Service as ServiceFirefox
from selenium.webdriver.remote.webelement import WebElement
Expand Down Expand Up @@ -96,25 +94,13 @@ def _start_firefox_driver(self, headless, options, profile):
result = Firefox(service=service, **kwargs)
return result
def start_chrome_impl(
self, url=None, headless=False, maximize=False, options=None,
capabilities=None
self, url=None, headless=False, maximize=False, options=None
):
chrome_driver = \
self._start_chrome_driver(headless, maximize, options, capabilities)
chrome_driver = self._start_chrome_driver(headless, maximize, options)
return self._start(chrome_driver, url)
def _start_chrome_driver(self, headless, maximize, options, capabilities):
def _start_chrome_driver(self, headless, maximize, options):
chrome_options = self._get_chrome_options(headless, maximize, options)
try:
result = Chrome(
options=chrome_options, desired_capabilities=capabilities
)
except WebDriverException:
# This usually happens when chromedriver is not on the PATH.
driver_path = install_matching_chromedriver()
result = Chrome(
options=chrome_options, desired_capabilities=capabilities,
service=ServiceChrome(driver_path)
)
result = Chrome(options=chrome_options)
atexit.register(self._kill_service, result.service)
return result
def _get_chrome_options(self, headless, maximize, options):
Expand Down
20 changes: 0 additions & 20 deletions helium/_impl/chromedriver.py

This file was deleted.

5 changes: 1 addition & 4 deletions requirements/base.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,2 @@
# Also update setup.py when you edit this file.
selenium>=4.9,<4.10
# Freeze webdriver-manager for security; It downloads and runs binaries from the
# internet. If it becomes malicious in the future, very bad things can happen.
webdriver-manager==4.0.1
selenium>=4.16.0
6 changes: 1 addition & 5 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,7 @@
packages = find_packages(exclude=['tests', 'tests.*']),
install_requires = [
# Also update requirements/base.txt when you make changes here.
'selenium>=4.9.0,<4.10',
# Freeze webdriver-manager for security; It downloads and runs binaries
# from the internet. If it becomes malicious in the future, very bad
# things can happen.
'webdriver-manager==4.0.1'
'selenium>=4.16.0'
],
package_data = {
'helium._impl': ['webdrivers/**/*']
Expand Down
18 changes: 0 additions & 18 deletions tests/unit/test__impl/test_chromedriver.py

This file was deleted.

0 comments on commit 6dcbbc9

Please sign in to comment.