Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/py/selenium/webdriver/chromium/options.py
1864 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 base64
19
import os
20
from typing import BinaryIO, Optional, Union
21
22
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
23
from selenium.webdriver.common.options import ArgOptions
24
25
26
class ChromiumOptions(ArgOptions):
27
KEY = "goog:chromeOptions"
28
29
def __init__(self) -> None:
30
super().__init__()
31
self._binary_location: str = ""
32
self._extension_files: list[str] = []
33
self._extensions: list[str] = []
34
self._experimental_options: dict[str, Union[str, int, dict, list[str]]] = {}
35
self._debugger_address: Optional[str] = None
36
self._enable_webextensions: bool = False
37
38
@property
39
def binary_location(self) -> str:
40
""":Returns: The location of the binary, otherwise an empty string."""
41
return self._binary_location
42
43
@binary_location.setter
44
def binary_location(self, value: str) -> None:
45
"""Allows you to set where the chromium binary lives.
46
47
Parameters:
48
----------
49
value: path to the Chromium binary
50
"""
51
if not isinstance(value, str):
52
raise TypeError(self.BINARY_LOCATION_ERROR)
53
self._binary_location = value
54
55
@property
56
def debugger_address(self) -> Optional[str]:
57
""":Returns: The address of the remote devtools instance."""
58
return self._debugger_address
59
60
@debugger_address.setter
61
def debugger_address(self, value: str) -> None:
62
"""Allows you to set the address of the remote devtools instance that
63
the ChromeDriver instance will try to connect to during an active wait.
64
65
Parameters:
66
----------
67
value: address of remote devtools instance if any (hostname[:port])
68
"""
69
if not isinstance(value, str):
70
raise TypeError("Debugger Address must be a string")
71
self._debugger_address = value
72
73
@property
74
def extensions(self) -> list[str]:
75
""":Returns: A list of encoded extensions that will be loaded."""
76
77
def _decode(file_data: BinaryIO) -> str:
78
# Should not use base64.encodestring() which inserts newlines every
79
# 76 characters (per RFC 1521). Chromedriver has to remove those
80
# unnecessary newlines before decoding, causing performance hit.
81
return base64.b64encode(file_data.read()).decode("utf-8")
82
83
encoded_extensions = []
84
for extension in self._extension_files:
85
with open(extension, "rb") as f:
86
encoded_extensions.append(_decode(f))
87
88
return encoded_extensions + self._extensions
89
90
def add_extension(self, extension: str) -> None:
91
"""Adds the path to the extension to a list that will be used to
92
extract it to the ChromeDriver.
93
94
Parameters:
95
----------
96
extension: path to the \\*.crx file
97
"""
98
if extension:
99
extension_to_add = os.path.abspath(os.path.expanduser(extension))
100
if os.path.exists(extension_to_add):
101
self._extension_files.append(extension_to_add)
102
else:
103
raise OSError("Path to the extension doesn't exist")
104
else:
105
raise ValueError("argument can not be null")
106
107
def add_encoded_extension(self, extension: str) -> None:
108
"""Adds Base64 encoded string with extension data to a list that will
109
be used to extract it to the ChromeDriver.
110
111
Parameters:
112
----------
113
extension: Base64 encoded string with extension data
114
"""
115
if extension:
116
self._extensions.append(extension)
117
else:
118
raise ValueError("argument can not be null")
119
120
@property
121
def experimental_options(self) -> dict:
122
""":Returns: A dictionary of experimental options for chromium."""
123
return self._experimental_options
124
125
def add_experimental_option(self, name: str, value: Union[str, int, dict, list[str]]) -> None:
126
"""Adds an experimental option which is passed to chromium.
127
128
Parameters:
129
----------
130
name: The experimental option name.
131
value: The option value.
132
"""
133
self._experimental_options[name] = value
134
135
@property
136
def enable_webextensions(self) -> bool:
137
""":Returns: Whether webextension support is enabled for Chromium-based browsers.
138
True if webextension support is enabled, False otherwise.
139
"""
140
return self._enable_webextensions
141
142
@enable_webextensions.setter
143
def enable_webextensions(self, value: bool) -> None:
144
"""Enables or disables webextension support for Chromium-based browsers.
145
146
Parameters:
147
----------
148
value : bool
149
True to enable webextension support, False to disable.
150
151
Notes:
152
-----
153
- When enabled, this automatically adds the required Chromium flags:
154
- --enable-unsafe-extension-debugging
155
- --remote-debugging-pipe
156
- When disabled, this removes BOTH flags listed above, even if they were manually added via add_argument()
157
before enabling webextensions.
158
- Enabling --remote-debugging-pipe makes the connection b/w chromedriver
159
and the browser use a pipe instead of a port, disabling many CDP functionalities
160
like devtools
161
"""
162
self._enable_webextensions = value
163
if value:
164
# Add required flags for Chromium webextension support
165
required_flags = ["--enable-unsafe-extension-debugging", "--remote-debugging-pipe"]
166
for flag in required_flags:
167
if flag not in self._arguments:
168
self.add_argument(flag)
169
else:
170
# Remove webextension flags if disabling
171
flags_to_remove = ["--enable-unsafe-extension-debugging", "--remote-debugging-pipe"]
172
for flag in flags_to_remove:
173
if flag in self._arguments:
174
self._arguments.remove(flag)
175
176
def to_capabilities(self) -> dict:
177
"""Creates a capabilities with all the options that have been set
178
179
Returns:
180
-------
181
dict : a dictionary with all set options
182
"""
183
caps = self._caps
184
chrome_options = self.experimental_options.copy()
185
if self.mobile_options:
186
chrome_options.update(self.mobile_options)
187
chrome_options["extensions"] = self.extensions
188
if self.binary_location:
189
chrome_options["binary"] = self.binary_location
190
chrome_options["args"] = self._arguments
191
if self.debugger_address:
192
chrome_options["debuggerAddress"] = self.debugger_address
193
194
caps[self.KEY] = chrome_options
195
196
return caps
197
198
@property
199
def default_capabilities(self) -> dict:
200
return DesiredCapabilities.CHROME.copy()
201
202