Skip to content

Commit

Permalink
Deprecate Webdriver executable_path argument, use selenium Service (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
jsfehler committed Jan 18, 2023
1 parent 9fcad0e commit 1d070fd
Show file tree
Hide file tree
Showing 8 changed files with 144 additions and 20 deletions.
28 changes: 21 additions & 7 deletions docs/drivers/chrome.rst
Original file line number Diff line number Diff line change
Expand Up @@ -71,20 +71,34 @@ Further Information: `chrome driver documentation <https://sites.google.com/a/ch
)
browser = Browser('chrome', options=chrome_options)
Service
+++++++

Selenium uses the Service class to manage chromedriver.
An instance of this class can be given directly to Splinter.

.. code-block:: python
from splinter import Browser
from selenium.webdriver.chrome.service import Service
my_service = Service()
browser = Browser('chrome', service=my_service)
Custom executable path
++++++++++++++++++++++
~~~~~~~~~~~~~~~~~~~~~~

Chrome can also be used from a custom path.
Pass the executable path as a dictionary to the `**kwargs` argument.
The dictionary should be set up with `executable_path` as the key and
the value set to the path to the executable file.
The Service object can be used to specify the path to chromedriver.
For example:

.. code-block:: python
from splinter import Browser
executable_path = {'executable_path':'</path/to/chrome>'}
from selenium.webdriver.chrome.service import Service
browser = Browser('chrome', **executable_path)
my_service = Service(executable_path='</path/to/chromedriver>')
browser = Browser('chrome', service=my_service)
API docs
Expand Down
29 changes: 22 additions & 7 deletions docs/drivers/edge.rst
Original file line number Diff line number Diff line change
Expand Up @@ -77,20 +77,35 @@ it is then possible to leverage the experimental emulation mode.
)
browser = Browser('edge', options=edge_options)
Service
+++++++

Selenium uses the Service class to manage edgedriver.
An instance of this class can be given directly to Splinter.

.. code-block:: python
from splinter import Browser
from selenium.webdriver.chrome.service import Service
my_service = Service()
browser = Browser('chrome', service=my_service)
Custom executable path
++++++++++++++++++++++
~~~~~~~~~~~~~~~~~~~~~~

Edge can also be used from a custom path.
Pass the executable path as a dictionary to the `**kwargs` argument.
The dictionary should be set up with `executable_path` as the key and
the value set to the path to the executable file.
The Service object can be used to specify the path to edgedriver.
For example:

.. code-block:: python
from splinter import Browser
executable_path = {'executable_path':'</path/to/edge>'}
from selenium.webdriver.edge.service import Service
my_service = Service(executable_path='</path/to/edgedriver>')
browser = Browser('edge', service=my_service)
browser = Browser('edge', **executable_path)
Edge Legacy
+++++++++++
Expand Down
30 changes: 30 additions & 0 deletions docs/drivers/firefox.rst
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,36 @@ when creating a Browser instance.
from splinter import Browser
browser = Browser('firefox', incognito=True)
Service
+++++++

Selenium uses the Service class to manage geckodriver.
An instance of this class can be given directly to Splinter.

.. code-block:: python
from splinter import Browser
from selenium.webdriver.firefox.service import Service
my_service = Service()
browser = Browser('firefox', service=my_service)
Custom executable path
~~~~~~~~~~~~~~~~~~~~~~

The Service object can be used to specify the path to geckodriver.
For example:

.. code-block:: python
from splinter import Browser
from selenium.webdriver.firefox.service import Service
my_service = Service(executable_path='</path/to/geckodriver>')
browser = Browser('firefox', service=my_service)
Specify Profile
+++++++++++++++

Expand Down
21 changes: 20 additions & 1 deletion splinter/driver/webdriver/chrome.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@
# Copyright 2012 splinter authors. All rights reserved.
# Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file.
import warnings
from typing import Optional

from selenium.webdriver import Chrome
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service

from splinter.driver.webdriver import BaseWebDriver


Expand All @@ -21,9 +25,24 @@ def __init__(
fullscreen=False,
incognito=False,
headless=False,
service: Optional[Service] = None,
**kwargs
):

if 'executable_path' in kwargs:
warnings.warn(
(
"Webdriver's executable_path argument has been deprecated."
"Please pass in a selenium Service object instead."
),
DeprecationWarning,
stacklevel=2,
)
if service is None:
service = Service(executable_path=kwargs['executable_path'])
else:
service.executable_path = kwargs['executable_path']

options = options or Options()

if user_agent is not None:
Expand All @@ -39,6 +58,6 @@ def __init__(
options.add_argument("--headless")
options.add_argument("--disable-gpu")

driver = Chrome(options=options, **kwargs)
driver = Chrome(options=options, service=service, **kwargs)

super(WebDriver, self).__init__(driver, wait_time)
21 changes: 19 additions & 2 deletions splinter/driver/webdriver/edge.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
# Copyright 2021 splinter authors. All rights reserved.
# Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file.

import warnings
from typing import Optional

from selenium.webdriver import Edge
from selenium.webdriver.edge.options import Options
from selenium.webdriver.edge.service import Service

from splinter.driver.webdriver import BaseWebDriver

Expand All @@ -24,9 +26,24 @@ def __init__(
incognito=False,
headless=False,
chromium=True,
service: Optional[Service] = None,
**kwargs
):

if 'executable_path' in kwargs:
warnings.warn(
(
"Webdriver's executable_path argument has been deprecated."
"Please pass in a selenium Service object instead."
),
DeprecationWarning,
stacklevel=2,
)
if service is None:
service = Service(executable_path=kwargs['executable_path'])
else:
service.executable_path = kwargs['executable_path']

options = Options() or options

if user_agent is not None:
Expand All @@ -44,6 +61,6 @@ def __init__(

options.use_chromium = chromium

driver = Edge(options=options, **kwargs)
driver = Edge(options=options, service=service, **kwargs)

super(WebDriver, self).__init__(driver, wait_time)
19 changes: 19 additions & 0 deletions splinter/driver/webdriver/firefox.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
# Copyright 2012 splinter authors. All rights reserved.
# Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file.
import warnings
from typing import Optional

from selenium.webdriver import Firefox
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.firefox.service import Service

from splinter.driver.webdriver import BaseWebDriver

Expand All @@ -26,9 +29,24 @@ def __init__(
capabilities=None,
headless=False,
incognito=False,
service: Optional[Service] = None,
**kwargs
):

if 'executable_path' in kwargs:
warnings.warn(
(
"Webdriver's executable_path argument has been deprecated."
"Please pass in a selenium Service object instead."
),
DeprecationWarning,
stacklevel=2,
)
if service is None:
service = Service(executable_path=kwargs['executable_path'])
else:
service.executable_path = kwargs['executable_path']

options = options or Options()
if profile:
options.set_preference("profile", profile)
Expand All @@ -54,6 +72,7 @@ def __init__(

driver = Firefox(
options=options,
service=service,
**kwargs,
)

Expand Down
6 changes: 4 additions & 2 deletions tests/get_browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,12 @@ def get_browser(browser_name, **kwargs):

elif browser_name == 'edge':
# Github Actions Windows EdgeDriver path
service = None
driver_path = os.getenv('EDGEWEBDRIVER')
if driver_path:
kwargs['executable_path'] = driver_path + '\msedgedriver.exe' # NOQA
from selenium.webdriver.edge.service import Service as EdgeService
service = EdgeService(executable_path=f'{driver_path}\msedgedriver.exe') # NOQA

return Browser('edge', headless=True, **kwargs)
return Browser('edge', headless=True, service=service, **kwargs)

raise ValueError('Unknown browser name')
10 changes: 9 additions & 1 deletion tests/test_webdriver.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,16 @@ def test_webdriver_local_driver_not_present(browser_name):
"""When chromedriver/geckodriver are not present on the system."""
from splinter import Browser

from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.firefox.service import Service as FirefoxService

if browser_name == 'chrome':
service = ChromeService(executable_path='failpath')
else:
service = FirefoxService(executable_path='failpath')

with pytest.raises(WebDriverException) as e:
Browser(browser_name, executable_path='failpath')
Browser(browser_name, service=service)

assert "Message: 'failpath' executable needs to be in PATH." in str(e.value)

Expand Down

0 comments on commit 1d070fd

Please sign in to comment.