Path: blob/trunk/py/selenium/webdriver/ie/options.py
4012 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.3031Any 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`5152When an attribute lookup happens:5354Example: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`5859When an attribute assignment happens:6061Example:62`self.browser_attach_timeout` = 3063`__set__` method sets/updates the value of the key `browserAttachTimeout` in `_options`64dictionary in `Options` class.65"""6667def __init__(self, name, expected_type):68self.name = name69self.expected_type = expected_type7071def __get__(self, obj, cls):72return obj._options.get(self.name)7374def __set__(self, obj, value) -> None:75if not isinstance(value, self.expected_type):76raise ValueError(f"{self.name} should be of type {self.expected_type.__name__}")7778if self.name == "elementScrollBehavior" and value not in [79ElementScrollBehavior.TOP,80ElementScrollBehavior.BOTTOM,81]:82raise ValueError("Element Scroll Behavior out of range.")83obj._options[self.name] = value848586class Options(ArgOptions):87KEY = "se:ieOptions"88SWITCHES = "ie.browserCommandLineSwitches"8990BROWSER_ATTACH_TIMEOUT = "browserAttachTimeout"91ELEMENT_SCROLL_BEHAVIOR = "elementScrollBehavior"92ENSURE_CLEAN_SESSION = "ie.ensureCleanSession"93FILE_UPLOAD_DIALOG_TIMEOUT = "ie.fileUploadDialogTimeout"94FORCE_CREATE_PROCESS_API = "ie.forceCreateProcessApi"95FORCE_SHELL_WINDOWS_API = "ie.forceShellWindowsApi"96FULL_PAGE_SCREENSHOT = "ie.enableFullPageScreenshot"97IGNORE_PROTECTED_MODE_SETTINGS = "ignoreProtectedModeSettings"98IGNORE_ZOOM_LEVEL = "ignoreZoomSetting"99INITIAL_BROWSER_URL = "initialBrowserUrl"100NATIVE_EVENTS = "nativeEvents"101PERSISTENT_HOVER = "enablePersistentHover"102REQUIRE_WINDOW_FOCUS = "requireWindowFocus"103USE_PER_PROCESS_PROXY = "ie.usePerProcessProxy"104USE_LEGACY_FILE_UPLOAD_DIALOG_HANDLING = "ie.useLegacyFileUploadDialogHandling"105ATTACH_TO_EDGE_CHROME = "ie.edgechromium"106EDGE_EXECUTABLE_PATH = "ie.edgepath"107IGNORE_PROCESS_MATCH = "ie.ignoreprocessmatch"108109# Creating descriptor objects for each of the above IE options110browser_attach_timeout = _IeOptionsDescriptor(BROWSER_ATTACH_TIMEOUT, int)111"""Gets and Sets `browser_attach_timeout`.112113Usage:114- Get: `self.browser_attach_timeout`115- Set: `self.browser_attach_timeout = value`116117Args:118value: int - Timeout in milliseconds.119"""120121element_scroll_behavior = _IeOptionsDescriptor(ELEMENT_SCROLL_BEHAVIOR, Enum)122"""Gets and Sets `element_scroll_behavior`.123124Usage:125- Get: `self.element_scroll_behavior`126- Set: `self.element_scroll_behavior = value`127128Args:129value: int - Either 0 (Top) or 1 (Bottom).130"""131132ensure_clean_session = _IeOptionsDescriptor(ENSURE_CLEAN_SESSION, bool)133"""Gets and Sets `ensure_clean_session`.134135Usage:136- Get: `self.ensure_clean_session`137- Set: `self.ensure_clean_session = value`138139Args:140value: bool141"""142143file_upload_dialog_timeout = _IeOptionsDescriptor(FILE_UPLOAD_DIALOG_TIMEOUT, int)144"""Gets and Sets `file_upload_dialog_timeout`.145146Usage:147- Get: `self.file_upload_dialog_timeout`148- Set: `self.file_upload_dialog_timeout = value`149150Args:151value: int - Timeout in milliseconds.152"""153154force_create_process_api = _IeOptionsDescriptor(FORCE_CREATE_PROCESS_API, bool)155"""Gets and Sets `force_create_process_api`.156157Usage:158- Get: `self.force_create_process_api`159- Set: `self.force_create_process_api = value`160161Args:162value: bool163"""164165force_shell_windows_api = _IeOptionsDescriptor(FORCE_SHELL_WINDOWS_API, bool)166"""Gets and Sets `force_shell_windows_api`.167168Usage:169- Get: `self.force_shell_windows_api`170- Set: `self.force_shell_windows_api = value`171172Args:173value: bool174"""175176full_page_screenshot = _IeOptionsDescriptor(FULL_PAGE_SCREENSHOT, bool)177"""Gets and Sets `full_page_screenshot`.178179Usage:180- Get: `self.full_page_screenshot`181- Set: `self.full_page_screenshot = value`182183Args:184value: bool185"""186187ignore_protected_mode_settings = _IeOptionsDescriptor(IGNORE_PROTECTED_MODE_SETTINGS, bool)188"""Gets and Sets `ignore_protected_mode_settings`.189190Usage:191- Get: `self.ignore_protected_mode_settings`192- Set: `self.ignore_protected_mode_settings = value`193194Args:195value: bool196"""197198ignore_zoom_level = _IeOptionsDescriptor(IGNORE_ZOOM_LEVEL, bool)199"""Gets and Sets `ignore_zoom_level`.200201Usage:202- Get: `self.ignore_zoom_level`203- Set: `self.ignore_zoom_level = value`204205Args:206value: bool207"""208209initial_browser_url = _IeOptionsDescriptor(INITIAL_BROWSER_URL, str)210"""Gets and Sets `initial_browser_url`.211212Usage:213- Get: `self.initial_browser_url`214- Set: `self.initial_browser_url = value`215216Args:217value: str218"""219220native_events = _IeOptionsDescriptor(NATIVE_EVENTS, bool)221"""Gets and Sets `native_events`.222223Usage:224- Get: `self.native_events`225- Set: `self.native_events = value`226227Args:228value: bool229"""230231persistent_hover = _IeOptionsDescriptor(PERSISTENT_HOVER, bool)232"""Gets and Sets `persistent_hover`.233234Usage:235- Get: `self.persistent_hover`236- Set: `self.persistent_hover = value`237238Args:239value: bool240"""241242require_window_focus = _IeOptionsDescriptor(REQUIRE_WINDOW_FOCUS, bool)243"""Gets and Sets `require_window_focus`.244245Usage:246- Get: `self.require_window_focus`247- Set: `self.require_window_focus = value`248249Args:250value: bool251"""252253use_per_process_proxy = _IeOptionsDescriptor(USE_PER_PROCESS_PROXY, bool)254"""Gets and Sets `use_per_process_proxy`.255256Usage:257- Get: `self.use_per_process_proxy`258- Set: `self.use_per_process_proxy = value`259260Args:261value: bool262"""263264use_legacy_file_upload_dialog_handling = _IeOptionsDescriptor(USE_LEGACY_FILE_UPLOAD_DIALOG_HANDLING, bool)265"""Gets and Sets `use_legacy_file_upload_dialog_handling`.266267Usage:268- Get: `self.use_legacy_file_upload_dialog_handling`269- Set: `self.use_legacy_file_upload_dialog_handling = value`270271Args:272value: bool273"""274275attach_to_edge_chrome = _IeOptionsDescriptor(ATTACH_TO_EDGE_CHROME, bool)276"""Gets and Sets `attach_to_edge_chrome`.277278Usage:279- Get: `self.attach_to_edge_chrome`280- Set: `self.attach_to_edge_chrome = value`281282Args:283value: bool284"""285286edge_executable_path = _IeOptionsDescriptor(EDGE_EXECUTABLE_PATH, str)287"""Gets and Sets `edge_executable_path`.288289Usage:290- Get: `self.edge_executable_path`291- Set: `self.edge_executable_path = value`292293Args:294value: str295"""296297def __init__(self) -> None:298super().__init__()299self._options: dict[str, Any] = {}300self._additional: dict[str, Any] = {}301302@property303def options(self) -> dict:304"""Returns a dictionary of browser options."""305return self._options306307@property308def additional_options(self) -> dict:309"""Returns the additional options."""310return self._additional311312def add_additional_option(self, name: str, value) -> None:313"""Adds an additional option not yet added as a safe option for IE.314315Args:316name: name of the option to add317value: value of the option to add318"""319self._additional[name] = value320321def to_capabilities(self) -> dict:322"""Marshals the IE options to the correct object."""323caps = self._caps324325opts = self._options.copy()326if self._arguments:327opts[self.SWITCHES] = " ".join(self._arguments)328329if self._additional:330opts.update(self._additional)331332if opts:333caps[Options.KEY] = opts334return caps335336@property337def default_capabilities(self) -> dict:338return DesiredCapabilities.INTERNETEXPLORER.copy()339340341