Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/py/selenium/webdriver/ie/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
from enum import Enum
18
from typing import Any
19
20
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
21
from selenium.webdriver.common.options import ArgOptions
22
23
24
class ElementScrollBehavior(Enum):
25
TOP = 0
26
BOTTOM = 1
27
28
29
class _IeOptionsDescriptor:
30
"""_IeOptionsDescriptor is an implementation of Descriptor Protocol.
31
32
Any look-up or assignment to the below attributes in `Options` class will be intercepted
33
by `__get__` and `__set__` method respectively.
34
35
- `browser_attach_timeout`
36
- `element_scroll_behavior`
37
- `ensure_clean_session`
38
- `file_upload_dialog_timeout`
39
- `force_create_process_api`
40
- `force_shell_windows_api`
41
- `full_page_screenshot`
42
- `ignore_protected_mode_settings`
43
- `ignore_zoom_level`
44
- `initial_browser_url`
45
- `native_events`
46
- `persistent_hover`
47
- `require_window_focus`
48
- `use_per_process_proxy`
49
- `use_legacy_file_upload_dialog_handling`
50
- `attach_to_edge_chrome`
51
- `edge_executable_path`
52
53
When an attribute lookup happens:
54
55
Example:
56
`self. browser_attach_timeout`
57
`__get__` method does a dictionary look up in the dictionary `_options` in `Options` class
58
and returns the value of key `browserAttachTimeout`
59
60
When an attribute assignment happens:
61
62
Example:
63
`self.browser_attach_timeout` = 30
64
`__set__` method sets/updates the value of the key `browserAttachTimeout` in `_options`
65
dictionary in `Options` class.
66
"""
67
68
def __init__(self, name, expected_type):
69
self.name = name
70
self.expected_type = expected_type
71
72
def __get__(self, obj, cls):
73
return obj._options.get(self.name)
74
75
def __set__(self, obj, value) -> None:
76
if not isinstance(value, self.expected_type):
77
raise ValueError(f"{self.name} should be of type {self.expected_type.__name__}")
78
79
if self.name == "elementScrollBehavior" and value not in [
80
ElementScrollBehavior.TOP,
81
ElementScrollBehavior.BOTTOM,
82
]:
83
raise ValueError("Element Scroll Behavior out of range.")
84
obj._options[self.name] = value
85
86
87
class Options(ArgOptions):
88
KEY = "se:ieOptions"
89
SWITCHES = "ie.browserCommandLineSwitches"
90
91
BROWSER_ATTACH_TIMEOUT = "browserAttachTimeout"
92
ELEMENT_SCROLL_BEHAVIOR = "elementScrollBehavior"
93
ENSURE_CLEAN_SESSION = "ie.ensureCleanSession"
94
FILE_UPLOAD_DIALOG_TIMEOUT = "ie.fileUploadDialogTimeout"
95
FORCE_CREATE_PROCESS_API = "ie.forceCreateProcessApi"
96
FORCE_SHELL_WINDOWS_API = "ie.forceShellWindowsApi"
97
FULL_PAGE_SCREENSHOT = "ie.enableFullPageScreenshot"
98
IGNORE_PROTECTED_MODE_SETTINGS = "ignoreProtectedModeSettings"
99
IGNORE_ZOOM_LEVEL = "ignoreZoomSetting"
100
INITIAL_BROWSER_URL = "initialBrowserUrl"
101
NATIVE_EVENTS = "nativeEvents"
102
PERSISTENT_HOVER = "enablePersistentHover"
103
REQUIRE_WINDOW_FOCUS = "requireWindowFocus"
104
USE_PER_PROCESS_PROXY = "ie.usePerProcessProxy"
105
USE_LEGACY_FILE_UPLOAD_DIALOG_HANDLING = "ie.useLegacyFileUploadDialogHandling"
106
ATTACH_TO_EDGE_CHROME = "ie.edgechromium"
107
EDGE_EXECUTABLE_PATH = "ie.edgepath"
108
IGNORE_PROCESS_MATCH = "ie.ignoreprocessmatch"
109
110
# Creating descriptor objects for each of the above IE options
111
browser_attach_timeout = _IeOptionsDescriptor(BROWSER_ATTACH_TIMEOUT, int)
112
"""Gets and Sets `browser_attach_timeout`.
113
114
Usage:
115
- Get: `self.browser_attach_timeout`
116
- Set: `self.browser_attach_timeout = value`
117
118
Args:
119
value: int - Timeout in milliseconds.
120
"""
121
122
element_scroll_behavior = _IeOptionsDescriptor(ELEMENT_SCROLL_BEHAVIOR, Enum)
123
"""Gets and Sets `element_scroll_behavior`.
124
125
Usage:
126
- Get: `self.element_scroll_behavior`
127
- Set: `self.element_scroll_behavior = value`
128
129
Args:
130
value: int - Either 0 (Top) or 1 (Bottom).
131
"""
132
133
ensure_clean_session = _IeOptionsDescriptor(ENSURE_CLEAN_SESSION, bool)
134
"""Gets and Sets `ensure_clean_session`.
135
136
Usage:
137
- Get: `self.ensure_clean_session`
138
- Set: `self.ensure_clean_session = value`
139
140
Args:
141
value: bool
142
"""
143
144
file_upload_dialog_timeout = _IeOptionsDescriptor(FILE_UPLOAD_DIALOG_TIMEOUT, int)
145
"""Gets and Sets `file_upload_dialog_timeout`.
146
147
Usage:
148
- Get: `self.file_upload_dialog_timeout`
149
- Set: `self.file_upload_dialog_timeout = value`
150
151
Args:
152
value: int - Timeout in milliseconds.
153
"""
154
155
force_create_process_api = _IeOptionsDescriptor(FORCE_CREATE_PROCESS_API, bool)
156
"""Gets and Sets `force_create_process_api`.
157
158
Usage:
159
- Get: `self.force_create_process_api`
160
- Set: `self.force_create_process_api = value`
161
162
Args:
163
value: bool
164
"""
165
166
force_shell_windows_api = _IeOptionsDescriptor(FORCE_SHELL_WINDOWS_API, bool)
167
"""Gets and Sets `force_shell_windows_api`.
168
169
Usage:
170
- Get: `self.force_shell_windows_api`
171
- Set: `self.force_shell_windows_api = value`
172
173
Args:
174
value: bool
175
"""
176
177
full_page_screenshot = _IeOptionsDescriptor(FULL_PAGE_SCREENSHOT, bool)
178
"""Gets and Sets `full_page_screenshot`.
179
180
Usage:
181
- Get: `self.full_page_screenshot`
182
- Set: `self.full_page_screenshot = value`
183
184
Args:
185
value: bool
186
"""
187
188
ignore_protected_mode_settings = _IeOptionsDescriptor(IGNORE_PROTECTED_MODE_SETTINGS, bool)
189
"""Gets and Sets `ignore_protected_mode_settings`.
190
191
Usage:
192
- Get: `self.ignore_protected_mode_settings`
193
- Set: `self.ignore_protected_mode_settings = value`
194
195
Args:
196
value: bool
197
"""
198
199
ignore_zoom_level = _IeOptionsDescriptor(IGNORE_ZOOM_LEVEL, bool)
200
"""Gets and Sets `ignore_zoom_level`.
201
202
Usage:
203
- Get: `self.ignore_zoom_level`
204
- Set: `self.ignore_zoom_level = value`
205
206
Args:
207
value: bool
208
"""
209
210
initial_browser_url = _IeOptionsDescriptor(INITIAL_BROWSER_URL, str)
211
"""Gets and Sets `initial_browser_url`.
212
213
Usage:
214
- Get: `self.initial_browser_url`
215
- Set: `self.initial_browser_url = value`
216
217
Args:
218
value: str
219
"""
220
221
native_events = _IeOptionsDescriptor(NATIVE_EVENTS, bool)
222
"""Gets and Sets `native_events`.
223
224
Usage:
225
- Get: `self.native_events`
226
- Set: `self.native_events = value`
227
228
Args:
229
value: bool
230
"""
231
232
persistent_hover = _IeOptionsDescriptor(PERSISTENT_HOVER, bool)
233
"""Gets and Sets `persistent_hover`.
234
235
Usage:
236
- Get: `self.persistent_hover`
237
- Set: `self.persistent_hover = value`
238
239
Args:
240
value: bool
241
"""
242
243
require_window_focus = _IeOptionsDescriptor(REQUIRE_WINDOW_FOCUS, bool)
244
"""Gets and Sets `require_window_focus`.
245
246
Usage:
247
- Get: `self.require_window_focus`
248
- Set: `self.require_window_focus = value`
249
250
Args:
251
value: bool
252
"""
253
254
use_per_process_proxy = _IeOptionsDescriptor(USE_PER_PROCESS_PROXY, bool)
255
"""Gets and Sets `use_per_process_proxy`.
256
257
Usage:
258
- Get: `self.use_per_process_proxy`
259
- Set: `self.use_per_process_proxy = value`
260
261
Args:
262
value: bool
263
"""
264
265
use_legacy_file_upload_dialog_handling = _IeOptionsDescriptor(USE_LEGACY_FILE_UPLOAD_DIALOG_HANDLING, bool)
266
"""Gets and Sets `use_legacy_file_upload_dialog_handling`.
267
268
Usage:
269
- Get: `self.use_legacy_file_upload_dialog_handling`
270
- Set: `self.use_legacy_file_upload_dialog_handling = value`
271
272
Args:
273
value: bool
274
"""
275
276
attach_to_edge_chrome = _IeOptionsDescriptor(ATTACH_TO_EDGE_CHROME, bool)
277
"""Gets and Sets `attach_to_edge_chrome`.
278
279
Usage:
280
- Get: `self.attach_to_edge_chrome`
281
- Set: `self.attach_to_edge_chrome = value`
282
283
Args:
284
value: bool
285
"""
286
287
edge_executable_path = _IeOptionsDescriptor(EDGE_EXECUTABLE_PATH, str)
288
"""Gets and Sets `edge_executable_path`.
289
290
Usage:
291
- Get: `self.edge_executable_path`
292
- Set: `self.edge_executable_path = value`
293
294
Args:
295
value: str
296
"""
297
298
def __init__(self) -> None:
299
super().__init__()
300
self._options: dict[str, Any] = {}
301
self._additional: dict[str, Any] = {}
302
303
@property
304
def options(self) -> dict:
305
"""Returns a dictionary of browser options."""
306
return self._options
307
308
@property
309
def additional_options(self) -> dict:
310
"""Returns the additional options."""
311
return self._additional
312
313
def add_additional_option(self, name: str, value) -> None:
314
"""Adds an additional option not yet added as a safe option for IE.
315
316
Args:
317
name: name of the option to add
318
value: value of the option to add
319
"""
320
self._additional[name] = value
321
322
def to_capabilities(self) -> dict:
323
"""Marshals the IE options to the correct object."""
324
caps = self._caps
325
326
opts = self._options.copy()
327
if self._arguments:
328
opts[self.SWITCHES] = " ".join(self._arguments)
329
330
if self._additional:
331
opts.update(self._additional)
332
333
if opts:
334
caps[Options.KEY] = opts
335
return caps
336
337
@property
338
def default_capabilities(self) -> dict:
339
return DesiredCapabilities.INTERNETEXPLORER.copy()
340
341