Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/py/selenium/webdriver/common/driver_finder.py
1864 views
1
# Licensed to the Software Freedom Conservancy (SFC) under one
2
# or more contributor license agreements. See the NOTICE file
3
# distributed with this work for additional information
4
# regarding copyright ownership. The SFC licenses this file
5
# to you under the Apache License, Version 2.0 (the
6
# "License"); you may not use this file except in compliance
7
# with the License. You may obtain a copy of the License at
8
#
9
# http://www.apache.org/licenses/LICENSE-2.0
10
#
11
# Unless required by applicable law or agreed to in writing,
12
# software distributed under the License is distributed on an
13
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
# KIND, either express or implied. See the License for the
15
# specific language governing permissions and limitations
16
# under the License.
17
import logging
18
from pathlib import Path
19
20
from selenium.common.exceptions import NoSuchDriverException
21
from selenium.webdriver.common.options import BaseOptions
22
from selenium.webdriver.common.selenium_manager import SeleniumManager
23
from selenium.webdriver.common.service import Service
24
25
logger = logging.getLogger(__name__)
26
27
28
class DriverFinder:
29
"""A Driver finding class responsible for obtaining the correct driver and
30
associated browser.
31
32
:param service: instance of the driver service class.
33
:param options: instance of the browser options class.
34
"""
35
36
def __init__(self, service: Service, options: BaseOptions) -> None:
37
self._service = service
38
self._options = options
39
self._paths = {"driver_path": "", "browser_path": ""}
40
41
"""Utility to find if a given file is present and executable.
42
43
This implementation is still in beta, and may change.
44
"""
45
46
def get_browser_path(self) -> str:
47
return self._binary_paths()["browser_path"]
48
49
def get_driver_path(self) -> str:
50
return self._binary_paths()["driver_path"]
51
52
def _binary_paths(self) -> dict:
53
if self._paths["driver_path"]:
54
return self._paths
55
56
browser = self._options.capabilities["browserName"]
57
try:
58
path = self._service.path
59
if path:
60
logger.debug(
61
"Skipping Selenium Manager; path to %s driver specified in Service class: %s", browser, path
62
)
63
if not Path(path).is_file():
64
raise ValueError(f"The path is not a valid file: {path}")
65
self._paths["driver_path"] = path
66
else:
67
output = SeleniumManager().binary_paths(self._to_args())
68
if Path(output["driver_path"]).is_file():
69
self._paths["driver_path"] = output["driver_path"]
70
else:
71
raise ValueError(f"The driver path is not a valid file: {output['driver_path']}")
72
if Path(output["browser_path"]).is_file():
73
self._paths["browser_path"] = output["browser_path"]
74
else:
75
raise ValueError(f"The browser path is not a valid file: {output['browser_path']}")
76
except Exception as err:
77
msg = f"Unable to obtain driver for {browser}"
78
raise NoSuchDriverException(msg) from err
79
return self._paths
80
81
def _to_args(self) -> list:
82
args = ["--browser", self._options.capabilities["browserName"]]
83
84
if self._options.browser_version:
85
args.append("--browser-version")
86
args.append(str(self._options.browser_version))
87
88
binary_location = getattr(self._options, "binary_location", None)
89
if binary_location:
90
args.append("--browser-path")
91
args.append(str(binary_location))
92
93
proxy = self._options.proxy
94
if proxy and (proxy.http_proxy or proxy.ssl_proxy):
95
args.append("--proxy")
96
value = proxy.ssl_proxy if proxy.ssl_proxy else proxy.http_proxy
97
args.append(value)
98
99
return args
100
101