Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/py/selenium/webdriver/chromium/service.py
4066 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
18
import logging
19
import os
20
import sys
21
from collections.abc import Mapping, Sequence
22
from typing import IO, Any
23
24
from selenium.webdriver.common import service
25
26
27
class ChromiumService(service.Service):
28
"""Service class responsible for starting and stopping the ChromiumDriver WebDriver instance.
29
30
Args:
31
executable_path: (Optional) Install path of the executable.
32
port: (Optional) Port for the service to run on, defaults to 0 where the operating system will decide.
33
service_args: (Optional) Sequence of args to be passed to the subprocess when launching the executable.
34
log_output: (Optional) int representation of STDOUT/DEVNULL, any IO instance or String path to file.
35
env: (Optional) Mapping of environment variables for the new process, defaults to `os.environ`.
36
driver_path_env_key: (Optional) Environment variable to use to get the path to the driver executable.
37
"""
38
39
def __init__(
40
self,
41
executable_path: str | None = None,
42
port: int = 0,
43
service_args: Sequence[str] | None = None,
44
log_output: int | str | IO[Any] | None = None,
45
env: Mapping[str, str] | None = None,
46
driver_path_env_key: str | None = None,
47
**kwargs,
48
) -> None:
49
self._service_args = list(service_args or [])
50
driver_path_env_key = driver_path_env_key or "SE_CHROMEDRIVER"
51
52
if isinstance(log_output, str):
53
self._service_args.append(f"--log-path={log_output}")
54
self.log_output = None
55
else:
56
self.log_output = log_output
57
58
if os.environ.get("SE_DEBUG"):
59
has_arg_conflicts = any(x in arg for arg in self._service_args for x in ("log-level", "log-path", "silent"))
60
has_output_conflict = self.log_output is not None
61
if has_arg_conflicts or has_output_conflict:
62
logging.getLogger(__name__).warning(
63
"Environment Variable `SE_DEBUG` is set; "
64
"forcing ChromiumDriver --verbose and overriding log-level/log-output/silent settings."
65
)
66
if has_arg_conflicts:
67
self._service_args = [
68
arg for arg in self._service_args if not any(x in arg for x in ("log-level", "log-path", "silent"))
69
]
70
self._service_args.append("--verbose")
71
self.log_output = sys.stderr
72
73
super().__init__(
74
executable_path=executable_path,
75
port=port,
76
env=env,
77
log_output=self.log_output,
78
driver_path_env_key=driver_path_env_key,
79
**kwargs,
80
)
81
82
def command_line_args(self) -> list[str]:
83
return [f"--port={self.port}"] + self._service_args
84
85
@property
86
def service_args(self) -> Sequence[str]:
87
"""Returns the sequence of service arguments."""
88
return self._service_args
89
90
@service_args.setter
91
def service_args(self, value: Sequence[str]):
92
if isinstance(value, str) or not isinstance(value, Sequence):
93
raise TypeError("service_args must be a sequence")
94
self._service_args = list(value)
95
96