Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/py/selenium/webdriver/firefox/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 typing import Any, Optional, Union
18
19
from typing_extensions import deprecated
20
21
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
22
from selenium.webdriver.common.options import ArgOptions
23
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
24
from selenium.webdriver.firefox.firefox_profile import FirefoxProfile
25
26
27
class Log:
28
def __init__(self) -> None:
29
self.level = None
30
31
def to_capabilities(self) -> dict:
32
if self.level:
33
return {"log": {"level": self.level}}
34
return {}
35
36
37
class Options(ArgOptions):
38
KEY = "moz:firefoxOptions"
39
40
def __init__(self) -> None:
41
super().__init__()
42
self._binary_location = ""
43
self._preferences: dict = {}
44
# https://fxdx.dev/deprecating-cdp-support-in-firefox-embracing-the-future-with-webdriver-bidi/.
45
# Enable BiDi only
46
self._preferences["remote.active-protocols"] = 1
47
self._profile: Optional[FirefoxProfile] = None
48
self.log = Log()
49
50
@property
51
@deprecated("use binary_location instead")
52
def binary(self) -> FirefoxBinary:
53
"""Returns the FirefoxBinary instance."""
54
return FirefoxBinary(self._binary_location)
55
56
@binary.setter
57
@deprecated("use binary_location instead")
58
def binary(self, new_binary: Union[str, FirefoxBinary]) -> None:
59
"""Sets location of the browser binary, either by string or
60
``FirefoxBinary`` instance."""
61
if isinstance(new_binary, FirefoxBinary):
62
new_binary = new_binary._start_cmd
63
self.binary_location = str(new_binary)
64
65
@property
66
def binary_location(self) -> str:
67
""":Returns: The location of the binary."""
68
return self._binary_location
69
70
@binary_location.setter # noqa
71
def binary_location(self, value: str) -> None:
72
"""Sets the location of the browser binary by string."""
73
if not isinstance(value, str):
74
raise TypeError(self.BINARY_LOCATION_ERROR)
75
self._binary_location = value
76
77
@property
78
def preferences(self) -> dict:
79
""":Returns: A dict of preferences."""
80
return self._preferences
81
82
def set_preference(self, name: str, value: Union[str, int, bool]):
83
"""Sets a preference."""
84
self._preferences[name] = value
85
86
@property
87
def profile(self) -> Optional[FirefoxProfile]:
88
""":Returns: The Firefox profile to use."""
89
return self._profile
90
91
@profile.setter
92
def profile(self, new_profile: Union[str, FirefoxProfile]) -> None:
93
"""Sets location of the browser profile to use, either by string or
94
``FirefoxProfile``."""
95
if not isinstance(new_profile, FirefoxProfile):
96
new_profile = FirefoxProfile(new_profile)
97
self._profile = new_profile
98
99
def enable_mobile(
100
self, android_package: Optional[str] = "org.mozilla.firefox", android_activity=None, device_serial=None
101
):
102
super().enable_mobile(android_package, android_activity, device_serial)
103
104
def to_capabilities(self) -> dict:
105
"""Marshals the Firefox options to a `moz:firefoxOptions` object."""
106
# This intentionally looks at the internal properties
107
# so if a binary or profile has _not_ been set,
108
# it will defer to geckodriver to find the system Firefox
109
# and generate a fresh profile.
110
caps = self._caps
111
opts: dict[str, Any] = {}
112
113
if self._binary_location:
114
opts["binary"] = self._binary_location
115
if self._preferences:
116
opts["prefs"] = self._preferences
117
if self._profile:
118
opts["profile"] = self._profile.encoded
119
if self._arguments:
120
opts["args"] = self._arguments
121
if self.mobile_options:
122
opts.update(self.mobile_options)
123
124
opts.update(self.log.to_capabilities())
125
126
if opts:
127
caps[Options.KEY] = opts
128
129
return caps
130
131
@property
132
def default_capabilities(self) -> dict:
133
return DesiredCapabilities.FIREFOX.copy()
134
135