Path: blob/trunk/py/selenium/webdriver/ie/options.py
1864 views
# Licensed to the Software Freedom Conservancy (SFC) under one1# or more contributor license agreements. See the NOTICE file2# distributed with this work for additional information3# regarding copyright ownership. The SFC licenses this file4# to you under the Apache License, Version 2.0 (the5# "License"); you may not use this file except in compliance6# with the License. You may obtain a copy of the License at7#8# http://www.apache.org/licenses/LICENSE-2.09#10# Unless required by applicable law or agreed to in writing,11# software distributed under the License is distributed on an12# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY13# KIND, either express or implied. See the License for the14# specific language governing permissions and limitations15# under the License.16from enum import Enum17from typing import Any1819from selenium.webdriver.common.desired_capabilities import DesiredCapabilities20from selenium.webdriver.common.options import ArgOptions212223class ElementScrollBehavior(Enum):24TOP = 025BOTTOM = 1262728class _IeOptionsDescriptor:29"""_IeOptionsDescriptor is an implementation of Descriptor Protocol:3031: Any look-up or assignment to the below attributes in `Options` class will be intercepted32by `__get__` and `__set__` method respectively.3334- `browser_attach_timeout`35- `element_scroll_behavior`36- `ensure_clean_session`37- `file_upload_dialog_timeout`38- `force_create_process_api`39- `force_shell_windows_api`40- `full_page_screenshot`41- `ignore_protected_mode_settings`42- `ignore_zoom_level`43- `initial_browser_url`44- `native_events`45- `persistent_hover`46- `require_window_focus`47- `use_per_process_proxy`48- `use_legacy_file_upload_dialog_handling`49- `attach_to_edge_chrome`50- `edge_executable_path`515253: When an attribute lookup happens,54Example:55`self. browser_attach_timeout`56`__get__` method does a dictionary look up in the dictionary `_options` in `Options` class57and returns the value of key `browserAttachTimeout`58: When an attribute assignment happens,59Example:60`self.browser_attach_timeout` = 3061`__set__` method sets/updates the value of the key `browserAttachTimeout` in `_options`62dictionary in `Options` class.63"""6465def __init__(self, name, expected_type):66self.name = name67self.expected_type = expected_type6869def __get__(self, obj, cls):70return obj._options.get(self.name)7172def __set__(self, obj, value) -> None:73if not isinstance(value, self.expected_type):74raise ValueError(f"{self.name} should be of type {self.expected_type.__name__}")7576if self.name == "elementScrollBehavior" and value not in [77ElementScrollBehavior.TOP,78ElementScrollBehavior.BOTTOM,79]:80raise ValueError("Element Scroll Behavior out of range.")81obj._options[self.name] = value828384class Options(ArgOptions):85KEY = "se:ieOptions"86SWITCHES = "ie.browserCommandLineSwitches"8788BROWSER_ATTACH_TIMEOUT = "browserAttachTimeout"89ELEMENT_SCROLL_BEHAVIOR = "elementScrollBehavior"90ENSURE_CLEAN_SESSION = "ie.ensureCleanSession"91FILE_UPLOAD_DIALOG_TIMEOUT = "ie.fileUploadDialogTimeout"92FORCE_CREATE_PROCESS_API = "ie.forceCreateProcessApi"93FORCE_SHELL_WINDOWS_API = "ie.forceShellWindowsApi"94FULL_PAGE_SCREENSHOT = "ie.enableFullPageScreenshot"95IGNORE_PROTECTED_MODE_SETTINGS = "ignoreProtectedModeSettings"96IGNORE_ZOOM_LEVEL = "ignoreZoomSetting"97INITIAL_BROWSER_URL = "initialBrowserUrl"98NATIVE_EVENTS = "nativeEvents"99PERSISTENT_HOVER = "enablePersistentHover"100REQUIRE_WINDOW_FOCUS = "requireWindowFocus"101USE_PER_PROCESS_PROXY = "ie.usePerProcessProxy"102USE_LEGACY_FILE_UPLOAD_DIALOG_HANDLING = "ie.useLegacyFileUploadDialogHandling"103ATTACH_TO_EDGE_CHROME = "ie.edgechromium"104EDGE_EXECUTABLE_PATH = "ie.edgepath"105IGNORE_PROCESS_MATCH = "ie.ignoreprocessmatch"106107# Creating descriptor objects for each of the above IE options108browser_attach_timeout = _IeOptionsDescriptor(BROWSER_ATTACH_TIMEOUT, int)109"""Gets and Sets `browser_attach_timeout`110111Usage:112------113- Get114- `self.browser_attach_timeout`115- Set116- `self.browser_attach_timeout` = `value`117118Parameters:119-----------120`value`: `int` (Timeout) in milliseconds121"""122123element_scroll_behavior = _IeOptionsDescriptor(ELEMENT_SCROLL_BEHAVIOR, Enum)124"""Gets and Sets `element_scroll_behavior`125126Usage:127------128- Get129- `self.element_scroll_behavior`130- Set131- `self.element_scroll_behavior` = `value`132133Parameters:134-----------135`value`: `int` either 0 - Top, 1 - Bottom136"""137138ensure_clean_session = _IeOptionsDescriptor(ENSURE_CLEAN_SESSION, bool)139"""Gets and Sets `ensure_clean_session`140141Usage:142------143- Get144- `self.ensure_clean_session`145- Set146- `self.ensure_clean_session` = `value`147148Parameters:149-----------150`value`: `bool`151"""152153file_upload_dialog_timeout = _IeOptionsDescriptor(FILE_UPLOAD_DIALOG_TIMEOUT, int)154"""Gets and Sets `file_upload_dialog_timeout`155156Usage:157------158- Get159- `self.file_upload_dialog_timeout`160- Set161- `self.file_upload_dialog_timeout` = `value`162163Parameters:164-----------165`value`: `int` (Timeout) in milliseconds166"""167168force_create_process_api = _IeOptionsDescriptor(FORCE_CREATE_PROCESS_API, bool)169"""Gets and Sets `force_create_process_api`170171Usage:172------173- Get174- `self.force_create_process_api`175- Set176- `self.force_create_process_api` = `value`177178Parameters:179-----------180`value`: `bool`181"""182183force_shell_windows_api = _IeOptionsDescriptor(FORCE_SHELL_WINDOWS_API, bool)184"""Gets and Sets `force_shell_windows_api`185186Usage:187------188- Get189- `self.force_shell_windows_api`190- Set191- `self.force_shell_windows_api` = `value`192193Parameters:194-----------195`value`: `bool`196"""197198full_page_screenshot = _IeOptionsDescriptor(FULL_PAGE_SCREENSHOT, bool)199"""Gets and Sets `full_page_screenshot`200201Usage:202------203- Get204- `self.full_page_screenshot`205- Set206- `self.full_page_screenshot` = `value`207208Parameters:209-----------210`value`: `bool`211"""212213ignore_protected_mode_settings = _IeOptionsDescriptor(IGNORE_PROTECTED_MODE_SETTINGS, bool)214"""Gets and Sets `ignore_protected_mode_settings`215216Usage:217------218- Get219- `self.ignore_protected_mode_settings`220- Set221- `self.ignore_protected_mode_settings` = `value`222223Parameters:224-----------225`value`: `bool`226"""227228ignore_zoom_level = _IeOptionsDescriptor(IGNORE_ZOOM_LEVEL, bool)229"""Gets and Sets `ignore_zoom_level`230231Usage:232------233- Get234- `self.ignore_zoom_level`235- Set236- `self.ignore_zoom_level` = `value`237238Parameters:239-----------240`value`: `bool`241"""242243initial_browser_url = _IeOptionsDescriptor(INITIAL_BROWSER_URL, str)244"""Gets and Sets `initial_browser_url`245246Usage:247------248- Get249- `self.initial_browser_url`250- Set251- `self.initial_browser_url` = `value`252253Parameters:254-----------255`value`: `str`256"""257258native_events = _IeOptionsDescriptor(NATIVE_EVENTS, bool)259"""Gets and Sets `native_events`260261Usage:262------263- Get264- `self.native_events`265- Set266- `self.native_events` = `value`267268Parameters:269-----------270`value`: `bool`271"""272273persistent_hover = _IeOptionsDescriptor(PERSISTENT_HOVER, bool)274"""Gets and Sets `persistent_hover`275276Usage:277------278- Get279- `self.persistent_hover`280- Set281- `self.persistent_hover` = `value`282283Parameters:284-----------285`value`: `bool`286"""287288require_window_focus = _IeOptionsDescriptor(REQUIRE_WINDOW_FOCUS, bool)289"""Gets and Sets `require_window_focus`290291Usage:292------293- Get294- `self.require_window_focus`295- Set296- `self.require_window_focus` = `value`297298Parameters:299-----------300`value`: `bool`301"""302303use_per_process_proxy = _IeOptionsDescriptor(USE_PER_PROCESS_PROXY, bool)304"""Gets and Sets `use_per_process_proxy`305306Usage:307------308- Get309- `self.use_per_process_proxy`310- Set311- `self.use_per_process_proxy` = `value`312313Parameters:314-----------315`value`: `bool`316"""317318use_legacy_file_upload_dialog_handling = _IeOptionsDescriptor(USE_LEGACY_FILE_UPLOAD_DIALOG_HANDLING, bool)319"""Gets and Sets `use_legacy_file_upload_dialog_handling`320321Usage:322------323- Get324- `self.use_legacy_file_upload_dialog_handling`325- Set326- `self.use_legacy_file_upload_dialog_handling` = `value`327328Parameters:329-----------330`value`: `bool`331"""332333attach_to_edge_chrome = _IeOptionsDescriptor(ATTACH_TO_EDGE_CHROME, bool)334"""Gets and Sets `attach_to_edge_chrome`335336Usage:337------338- Get339- `self.attach_to_edge_chrome`340- Set341- `self.attach_to_edge_chrome` = `value`342343Parameters:344-----------345`value`: `bool`346"""347348edge_executable_path = _IeOptionsDescriptor(EDGE_EXECUTABLE_PATH, str)349"""Gets and Sets `edge_executable_path`350351Usage:352------353- Get354- `self.edge_executable_path`355- Set356- `self.edge_executable_path` = `value`357358Parameters:359-----------360`value`: `str`361"""362363def __init__(self) -> None:364super().__init__()365self._options: dict[str, Any] = {}366self._additional: dict[str, Any] = {}367368@property369def options(self) -> dict:370""":Returns: A dictionary of browser options."""371return self._options372373@property374def additional_options(self) -> dict:375""":Returns: The additional options."""376return self._additional377378def add_additional_option(self, name: str, value) -> None:379"""Adds an additional option not yet added as a safe option for IE.380381:Args:382- name: name of the option to add383- value: value of the option to add384"""385self._additional[name] = value386387def to_capabilities(self) -> dict:388"""Marshals the IE options to the correct object."""389caps = self._caps390391opts = self._options.copy()392if self._arguments:393opts[self.SWITCHES] = " ".join(self._arguments)394395if self._additional:396opts.update(self._additional)397398if opts:399caps[Options.KEY] = opts400return caps401402@property403def default_capabilities(self) -> dict:404return DesiredCapabilities.INTERNETEXPLORER.copy()405406407