Path: blob/trunk/py/selenium/webdriver/common/driver_finder.py
1864 views
# Licensed to the Software Freedom Conservancy (SFC) under one1# or more contributor license agreements. See the NOTICE file2# distributed with this work for additional information3# regarding copyright ownership. The SFC licenses this file4# to you under the Apache License, Version 2.0 (the5# "License"); you may not use this file except in compliance6# with the License. You may obtain a copy of the License at7#8# http://www.apache.org/licenses/LICENSE-2.09#10# Unless required by applicable law or agreed to in writing,11# software distributed under the License is distributed on an12# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY13# KIND, either express or implied. See the License for the14# specific language governing permissions and limitations15# under the License.16import logging17from pathlib import Path1819from selenium.common.exceptions import NoSuchDriverException20from selenium.webdriver.common.options import BaseOptions21from selenium.webdriver.common.selenium_manager import SeleniumManager22from selenium.webdriver.common.service import Service2324logger = logging.getLogger(__name__)252627class DriverFinder:28"""A Driver finding class responsible for obtaining the correct driver and29associated browser.3031:param service: instance of the driver service class.32:param options: instance of the browser options class.33"""3435def __init__(self, service: Service, options: BaseOptions) -> None:36self._service = service37self._options = options38self._paths = {"driver_path": "", "browser_path": ""}3940"""Utility to find if a given file is present and executable.4142This implementation is still in beta, and may change.43"""4445def get_browser_path(self) -> str:46return self._binary_paths()["browser_path"]4748def get_driver_path(self) -> str:49return self._binary_paths()["driver_path"]5051def _binary_paths(self) -> dict:52if self._paths["driver_path"]:53return self._paths5455browser = self._options.capabilities["browserName"]56try:57path = self._service.path58if path:59logger.debug(60"Skipping Selenium Manager; path to %s driver specified in Service class: %s", browser, path61)62if not Path(path).is_file():63raise ValueError(f"The path is not a valid file: {path}")64self._paths["driver_path"] = path65else:66output = SeleniumManager().binary_paths(self._to_args())67if Path(output["driver_path"]).is_file():68self._paths["driver_path"] = output["driver_path"]69else:70raise ValueError(f"The driver path is not a valid file: {output['driver_path']}")71if Path(output["browser_path"]).is_file():72self._paths["browser_path"] = output["browser_path"]73else:74raise ValueError(f"The browser path is not a valid file: {output['browser_path']}")75except Exception as err:76msg = f"Unable to obtain driver for {browser}"77raise NoSuchDriverException(msg) from err78return self._paths7980def _to_args(self) -> list:81args = ["--browser", self._options.capabilities["browserName"]]8283if self._options.browser_version:84args.append("--browser-version")85args.append(str(self._options.browser_version))8687binary_location = getattr(self._options, "binary_location", None)88if binary_location:89args.append("--browser-path")90args.append(str(binary_location))9192proxy = self._options.proxy93if proxy and (proxy.http_proxy or proxy.ssl_proxy):94args.append("--proxy")95value = proxy.ssl_proxy if proxy.ssl_proxy else proxy.http_proxy96args.append(value)9798return args99100101