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