Path: blob/trunk/py/selenium/webdriver/firefox/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 typing import Any, Optional, Union1718from typing_extensions import deprecated1920from selenium.webdriver.common.desired_capabilities import DesiredCapabilities21from selenium.webdriver.common.options import ArgOptions22from selenium.webdriver.firefox.firefox_binary import FirefoxBinary23from selenium.webdriver.firefox.firefox_profile import FirefoxProfile242526class Log:27def __init__(self) -> None:28self.level = None2930def to_capabilities(self) -> dict:31if self.level:32return {"log": {"level": self.level}}33return {}343536class Options(ArgOptions):37KEY = "moz:firefoxOptions"3839def __init__(self) -> None:40super().__init__()41self._binary_location = ""42self._preferences: dict = {}43# https://fxdx.dev/deprecating-cdp-support-in-firefox-embracing-the-future-with-webdriver-bidi/.44# Enable BiDi only45self._preferences["remote.active-protocols"] = 146self._profile: Optional[FirefoxProfile] = None47self.log = Log()4849@property50@deprecated("use binary_location instead")51def binary(self) -> FirefoxBinary:52"""Returns the FirefoxBinary instance."""53return FirefoxBinary(self._binary_location)5455@binary.setter56@deprecated("use binary_location instead")57def binary(self, new_binary: Union[str, FirefoxBinary]) -> None:58"""Sets location of the browser binary, either by string or59``FirefoxBinary`` instance."""60if isinstance(new_binary, FirefoxBinary):61new_binary = new_binary._start_cmd62self.binary_location = str(new_binary)6364@property65def binary_location(self) -> str:66""":Returns: The location of the binary."""67return self._binary_location6869@binary_location.setter # noqa70def binary_location(self, value: str) -> None:71"""Sets the location of the browser binary by string."""72if not isinstance(value, str):73raise TypeError(self.BINARY_LOCATION_ERROR)74self._binary_location = value7576@property77def preferences(self) -> dict:78""":Returns: A dict of preferences."""79return self._preferences8081def set_preference(self, name: str, value: Union[str, int, bool]):82"""Sets a preference."""83self._preferences[name] = value8485@property86def profile(self) -> Optional[FirefoxProfile]:87""":Returns: The Firefox profile to use."""88return self._profile8990@profile.setter91def profile(self, new_profile: Union[str, FirefoxProfile]) -> None:92"""Sets location of the browser profile to use, either by string or93``FirefoxProfile``."""94if not isinstance(new_profile, FirefoxProfile):95new_profile = FirefoxProfile(new_profile)96self._profile = new_profile9798def enable_mobile(99self, android_package: Optional[str] = "org.mozilla.firefox", android_activity=None, device_serial=None100):101super().enable_mobile(android_package, android_activity, device_serial)102103def to_capabilities(self) -> dict:104"""Marshals the Firefox options to a `moz:firefoxOptions` object."""105# This intentionally looks at the internal properties106# so if a binary or profile has _not_ been set,107# it will defer to geckodriver to find the system Firefox108# and generate a fresh profile.109caps = self._caps110opts: dict[str, Any] = {}111112if self._binary_location:113opts["binary"] = self._binary_location114if self._preferences:115opts["prefs"] = self._preferences116if self._profile:117opts["profile"] = self._profile.encoded118if self._arguments:119opts["args"] = self._arguments120if self.mobile_options:121opts.update(self.mobile_options)122123opts.update(self.log.to_capabilities())124125if opts:126caps[Options.KEY] = opts127128return caps129130@property131def default_capabilities(self) -> dict:132return DesiredCapabilities.FIREFOX.copy()133134135