Path: blob/trunk/py/selenium/webdriver/firefox/service.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.1617from collections.abc import Mapping, Sequence18from typing import Optional1920from selenium.types import SubprocessStdAlias21from selenium.webdriver.common import service, utils222324class Service(service.Service):25"""A Service class that is responsible for the starting and stopping of26`geckodriver`.2728:param executable_path: install path of the geckodriver executable, defaults to `geckodriver`.29:param port: Port for the service to run on, defaults to 0 where the operating system will decide.30:param service_args: (Optional) Sequence of args to be passed to the subprocess when launching the executable.31:param log_output: (Optional) int representation of STDOUT/DEVNULL, any IO instance or String path to file.32:param env: (Optional) Mapping of environment variables for the new process, defaults to `os.environ`.33:param driver_path_env_key: (Optional) Environment variable to use to get the path to the driver executable.34"""3536def __init__(37self,38executable_path: Optional[str] = None,39port: int = 0,40service_args: Optional[Sequence[str]] = None,41log_output: Optional[SubprocessStdAlias] = None,42env: Optional[Mapping[str, str]] = None,43driver_path_env_key: Optional[str] = None,44**kwargs,45) -> None:46self._service_args = list(service_args or [])47driver_path_env_key = driver_path_env_key or "SE_GECKODRIVER"4849super().__init__(50executable_path=executable_path,51port=port,52log_output=log_output,53env=env,54driver_path_env_key=driver_path_env_key,55**kwargs,56)5758# Set a port for CDP59if "--connect-existing" not in self._service_args:60self._service_args.append("--websocket-port")61self._service_args.append(f"{utils.free_port()}")6263def command_line_args(self) -> list[str]:64return ["--port", f"{self.port}"] + self._service_args6566@property67def service_args(self) -> Sequence[str]:68return self._service_args6970@service_args.setter71def service_args(self, value: Sequence[str]):72if isinstance(value, str) or not isinstance(value, Sequence):73raise TypeError("service_args must be a sequence")74self._service_args = list(value)757677