Path: blob/trunk/py/selenium/webdriver/chromium/service.py
4066 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.1617import logging18import os19import sys20from collections.abc import Mapping, Sequence21from typing import IO, Any2223from selenium.webdriver.common import service242526class ChromiumService(service.Service):27"""Service class responsible for starting and stopping the ChromiumDriver WebDriver instance.2829Args:30executable_path: (Optional) Install path of the executable.31port: (Optional) Port for the service to run on, defaults to 0 where the operating system will decide.32service_args: (Optional) Sequence of args to be passed to the subprocess when launching the executable.33log_output: (Optional) int representation of STDOUT/DEVNULL, any IO instance or String path to file.34env: (Optional) Mapping of environment variables for the new process, defaults to `os.environ`.35driver_path_env_key: (Optional) Environment variable to use to get the path to the driver executable.36"""3738def __init__(39self,40executable_path: str | None = None,41port: int = 0,42service_args: Sequence[str] | None = None,43log_output: int | str | IO[Any] | None = None,44env: Mapping[str, str] | None = None,45driver_path_env_key: str | None = None,46**kwargs,47) -> None:48self._service_args = list(service_args or [])49driver_path_env_key = driver_path_env_key or "SE_CHROMEDRIVER"5051if isinstance(log_output, str):52self._service_args.append(f"--log-path={log_output}")53self.log_output = None54else:55self.log_output = log_output5657if os.environ.get("SE_DEBUG"):58has_arg_conflicts = any(x in arg for arg in self._service_args for x in ("log-level", "log-path", "silent"))59has_output_conflict = self.log_output is not None60if has_arg_conflicts or has_output_conflict:61logging.getLogger(__name__).warning(62"Environment Variable `SE_DEBUG` is set; "63"forcing ChromiumDriver --verbose and overriding log-level/log-output/silent settings."64)65if has_arg_conflicts:66self._service_args = [67arg for arg in self._service_args if not any(x in arg for x in ("log-level", "log-path", "silent"))68]69self._service_args.append("--verbose")70self.log_output = sys.stderr7172super().__init__(73executable_path=executable_path,74port=port,75env=env,76log_output=self.log_output,77driver_path_env_key=driver_path_env_key,78**kwargs,79)8081def command_line_args(self) -> list[str]:82return [f"--port={self.port}"] + self._service_args8384@property85def service_args(self) -> Sequence[str]:86"""Returns the sequence of service arguments."""87return self._service_args8889@service_args.setter90def service_args(self, value: Sequence[str]):91if isinstance(value, str) or not isinstance(value, Sequence):92raise TypeError("service_args must be a sequence")93self._service_args = list(value)949596