Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/py/selenium/webdriver/firefox/service.py
4012 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, utils
25
26
27
class Service(service.Service):
28
"""Service class responsible for starting and stopping of `geckodriver`.
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_GECKODRIVER"
51
52
if os.environ.get("SE_DEBUG"):
53
has_log_arg = "--log" in self._service_args or any(arg.startswith("--log=") for arg in self._service_args)
54
has_output_conflict = log_output is not None
55
if has_log_arg or has_output_conflict:
56
logging.getLogger(__name__).warning(
57
"Environment Variable `SE_DEBUG` is set; "
58
"forcing GeckoDriver log level to DEBUG and overriding configured log level/output."
59
)
60
if has_log_arg:
61
if "--log" in self._service_args:
62
idx = self._service_args.index("--log")
63
del self._service_args[idx : idx + 2]
64
else:
65
self._service_args = [arg for arg in self._service_args if not arg.startswith("--log=")]
66
self._service_args.append("--log")
67
self._service_args.append("debug")
68
log_output = sys.stderr
69
70
super().__init__(
71
executable_path=executable_path,
72
port=port,
73
log_output=log_output,
74
env=env,
75
driver_path_env_key=driver_path_env_key,
76
**kwargs,
77
)
78
79
# Set a port for CDP
80
if "--connect-existing" not in self._service_args:
81
self._service_args.append("--websocket-port")
82
self._service_args.append(f"{utils.free_port()}")
83
84
def command_line_args(self) -> list[str]:
85
return ["--port", f"{self.port}"] + self._service_args
86
87
@property
88
def service_args(self) -> Sequence[str]:
89
"""Returns the sequence of service arguments."""
90
return self._service_args
91
92
@service_args.setter
93
def service_args(self, value: Sequence[str]):
94
if isinstance(value, str) or not isinstance(value, Sequence):
95
raise TypeError("service_args must be a sequence")
96
self._service_args = list(value)
97
98