Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/py/selenium/webdriver/ie/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
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
54
: When an attribute lookup happens,
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
: When an attribute assignment happens,
60
Example:
61
`self.browser_attach_timeout` = 30
62
`__set__` method sets/updates the value of the key `browserAttachTimeout` in `_options`
63
dictionary in `Options` class.
64
"""
65
66
def __init__(self, name, expected_type):
67
self.name = name
68
self.expected_type = expected_type
69
70
def __get__(self, obj, cls):
71
return obj._options.get(self.name)
72
73
def __set__(self, obj, value) -> None:
74
if not isinstance(value, self.expected_type):
75
raise ValueError(f"{self.name} should be of type {self.expected_type.__name__}")
76
77
if self.name == "elementScrollBehavior" and value not in [
78
ElementScrollBehavior.TOP,
79
ElementScrollBehavior.BOTTOM,
80
]:
81
raise ValueError("Element Scroll Behavior out of range.")
82
obj._options[self.name] = value
83
84
85
class Options(ArgOptions):
86
KEY = "se:ieOptions"
87
SWITCHES = "ie.browserCommandLineSwitches"
88
89
BROWSER_ATTACH_TIMEOUT = "browserAttachTimeout"
90
ELEMENT_SCROLL_BEHAVIOR = "elementScrollBehavior"
91
ENSURE_CLEAN_SESSION = "ie.ensureCleanSession"
92
FILE_UPLOAD_DIALOG_TIMEOUT = "ie.fileUploadDialogTimeout"
93
FORCE_CREATE_PROCESS_API = "ie.forceCreateProcessApi"
94
FORCE_SHELL_WINDOWS_API = "ie.forceShellWindowsApi"
95
FULL_PAGE_SCREENSHOT = "ie.enableFullPageScreenshot"
96
IGNORE_PROTECTED_MODE_SETTINGS = "ignoreProtectedModeSettings"
97
IGNORE_ZOOM_LEVEL = "ignoreZoomSetting"
98
INITIAL_BROWSER_URL = "initialBrowserUrl"
99
NATIVE_EVENTS = "nativeEvents"
100
PERSISTENT_HOVER = "enablePersistentHover"
101
REQUIRE_WINDOW_FOCUS = "requireWindowFocus"
102
USE_PER_PROCESS_PROXY = "ie.usePerProcessProxy"
103
USE_LEGACY_FILE_UPLOAD_DIALOG_HANDLING = "ie.useLegacyFileUploadDialogHandling"
104
ATTACH_TO_EDGE_CHROME = "ie.edgechromium"
105
EDGE_EXECUTABLE_PATH = "ie.edgepath"
106
IGNORE_PROCESS_MATCH = "ie.ignoreprocessmatch"
107
108
# Creating descriptor objects for each of the above IE options
109
browser_attach_timeout = _IeOptionsDescriptor(BROWSER_ATTACH_TIMEOUT, int)
110
"""Gets and Sets `browser_attach_timeout`
111
112
Usage:
113
------
114
- Get
115
- `self.browser_attach_timeout`
116
- Set
117
- `self.browser_attach_timeout` = `value`
118
119
Parameters:
120
-----------
121
`value`: `int` (Timeout) in milliseconds
122
"""
123
124
element_scroll_behavior = _IeOptionsDescriptor(ELEMENT_SCROLL_BEHAVIOR, Enum)
125
"""Gets and Sets `element_scroll_behavior`
126
127
Usage:
128
------
129
- Get
130
- `self.element_scroll_behavior`
131
- Set
132
- `self.element_scroll_behavior` = `value`
133
134
Parameters:
135
-----------
136
`value`: `int` either 0 - Top, 1 - Bottom
137
"""
138
139
ensure_clean_session = _IeOptionsDescriptor(ENSURE_CLEAN_SESSION, bool)
140
"""Gets and Sets `ensure_clean_session`
141
142
Usage:
143
------
144
- Get
145
- `self.ensure_clean_session`
146
- Set
147
- `self.ensure_clean_session` = `value`
148
149
Parameters:
150
-----------
151
`value`: `bool`
152
"""
153
154
file_upload_dialog_timeout = _IeOptionsDescriptor(FILE_UPLOAD_DIALOG_TIMEOUT, int)
155
"""Gets and Sets `file_upload_dialog_timeout`
156
157
Usage:
158
------
159
- Get
160
- `self.file_upload_dialog_timeout`
161
- Set
162
- `self.file_upload_dialog_timeout` = `value`
163
164
Parameters:
165
-----------
166
`value`: `int` (Timeout) in milliseconds
167
"""
168
169
force_create_process_api = _IeOptionsDescriptor(FORCE_CREATE_PROCESS_API, bool)
170
"""Gets and Sets `force_create_process_api`
171
172
Usage:
173
------
174
- Get
175
- `self.force_create_process_api`
176
- Set
177
- `self.force_create_process_api` = `value`
178
179
Parameters:
180
-----------
181
`value`: `bool`
182
"""
183
184
force_shell_windows_api = _IeOptionsDescriptor(FORCE_SHELL_WINDOWS_API, bool)
185
"""Gets and Sets `force_shell_windows_api`
186
187
Usage:
188
------
189
- Get
190
- `self.force_shell_windows_api`
191
- Set
192
- `self.force_shell_windows_api` = `value`
193
194
Parameters:
195
-----------
196
`value`: `bool`
197
"""
198
199
full_page_screenshot = _IeOptionsDescriptor(FULL_PAGE_SCREENSHOT, bool)
200
"""Gets and Sets `full_page_screenshot`
201
202
Usage:
203
------
204
- Get
205
- `self.full_page_screenshot`
206
- Set
207
- `self.full_page_screenshot` = `value`
208
209
Parameters:
210
-----------
211
`value`: `bool`
212
"""
213
214
ignore_protected_mode_settings = _IeOptionsDescriptor(IGNORE_PROTECTED_MODE_SETTINGS, bool)
215
"""Gets and Sets `ignore_protected_mode_settings`
216
217
Usage:
218
------
219
- Get
220
- `self.ignore_protected_mode_settings`
221
- Set
222
- `self.ignore_protected_mode_settings` = `value`
223
224
Parameters:
225
-----------
226
`value`: `bool`
227
"""
228
229
ignore_zoom_level = _IeOptionsDescriptor(IGNORE_ZOOM_LEVEL, bool)
230
"""Gets and Sets `ignore_zoom_level`
231
232
Usage:
233
------
234
- Get
235
- `self.ignore_zoom_level`
236
- Set
237
- `self.ignore_zoom_level` = `value`
238
239
Parameters:
240
-----------
241
`value`: `bool`
242
"""
243
244
initial_browser_url = _IeOptionsDescriptor(INITIAL_BROWSER_URL, str)
245
"""Gets and Sets `initial_browser_url`
246
247
Usage:
248
------
249
- Get
250
- `self.initial_browser_url`
251
- Set
252
- `self.initial_browser_url` = `value`
253
254
Parameters:
255
-----------
256
`value`: `str`
257
"""
258
259
native_events = _IeOptionsDescriptor(NATIVE_EVENTS, bool)
260
"""Gets and Sets `native_events`
261
262
Usage:
263
------
264
- Get
265
- `self.native_events`
266
- Set
267
- `self.native_events` = `value`
268
269
Parameters:
270
-----------
271
`value`: `bool`
272
"""
273
274
persistent_hover = _IeOptionsDescriptor(PERSISTENT_HOVER, bool)
275
"""Gets and Sets `persistent_hover`
276
277
Usage:
278
------
279
- Get
280
- `self.persistent_hover`
281
- Set
282
- `self.persistent_hover` = `value`
283
284
Parameters:
285
-----------
286
`value`: `bool`
287
"""
288
289
require_window_focus = _IeOptionsDescriptor(REQUIRE_WINDOW_FOCUS, bool)
290
"""Gets and Sets `require_window_focus`
291
292
Usage:
293
------
294
- Get
295
- `self.require_window_focus`
296
- Set
297
- `self.require_window_focus` = `value`
298
299
Parameters:
300
-----------
301
`value`: `bool`
302
"""
303
304
use_per_process_proxy = _IeOptionsDescriptor(USE_PER_PROCESS_PROXY, bool)
305
"""Gets and Sets `use_per_process_proxy`
306
307
Usage:
308
------
309
- Get
310
- `self.use_per_process_proxy`
311
- Set
312
- `self.use_per_process_proxy` = `value`
313
314
Parameters:
315
-----------
316
`value`: `bool`
317
"""
318
319
use_legacy_file_upload_dialog_handling = _IeOptionsDescriptor(USE_LEGACY_FILE_UPLOAD_DIALOG_HANDLING, bool)
320
"""Gets and Sets `use_legacy_file_upload_dialog_handling`
321
322
Usage:
323
------
324
- Get
325
- `self.use_legacy_file_upload_dialog_handling`
326
- Set
327
- `self.use_legacy_file_upload_dialog_handling` = `value`
328
329
Parameters:
330
-----------
331
`value`: `bool`
332
"""
333
334
attach_to_edge_chrome = _IeOptionsDescriptor(ATTACH_TO_EDGE_CHROME, bool)
335
"""Gets and Sets `attach_to_edge_chrome`
336
337
Usage:
338
------
339
- Get
340
- `self.attach_to_edge_chrome`
341
- Set
342
- `self.attach_to_edge_chrome` = `value`
343
344
Parameters:
345
-----------
346
`value`: `bool`
347
"""
348
349
edge_executable_path = _IeOptionsDescriptor(EDGE_EXECUTABLE_PATH, str)
350
"""Gets and Sets `edge_executable_path`
351
352
Usage:
353
------
354
- Get
355
- `self.edge_executable_path`
356
- Set
357
- `self.edge_executable_path` = `value`
358
359
Parameters:
360
-----------
361
`value`: `str`
362
"""
363
364
def __init__(self) -> None:
365
super().__init__()
366
self._options: dict[str, Any] = {}
367
self._additional: dict[str, Any] = {}
368
369
@property
370
def options(self) -> dict:
371
""":Returns: A dictionary of browser options."""
372
return self._options
373
374
@property
375
def additional_options(self) -> dict:
376
""":Returns: The additional options."""
377
return self._additional
378
379
def add_additional_option(self, name: str, value) -> None:
380
"""Adds an additional option not yet added as a safe option for IE.
381
382
:Args:
383
- name: name of the option to add
384
- value: value of the option to add
385
"""
386
self._additional[name] = value
387
388
def to_capabilities(self) -> dict:
389
"""Marshals the IE options to the correct object."""
390
caps = self._caps
391
392
opts = self._options.copy()
393
if self._arguments:
394
opts[self.SWITCHES] = " ".join(self._arguments)
395
396
if self._additional:
397
opts.update(self._additional)
398
399
if opts:
400
caps[Options.KEY] = opts
401
return caps
402
403
@property
404
def default_capabilities(self) -> dict:
405
return DesiredCapabilities.INTERNETEXPLORER.copy()
406
407