Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/py/selenium/webdriver/common/bidi/webextension.py
4065 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
18
19
from selenium.common.exceptions import WebDriverException
20
from selenium.webdriver.common.bidi.common import command_builder
21
22
23
class WebExtension:
24
"""BiDi implementation of the webExtension module."""
25
26
def __init__(self, conn):
27
self.conn = conn
28
29
def install(self, path=None, archive_path=None, base64_value=None) -> dict:
30
"""Installs a web extension in the remote end.
31
32
You must provide exactly one of the parameters.
33
34
Args:
35
path: Path to an extension directory.
36
archive_path: Path to an extension archive file.
37
base64_value: Base64 encoded string of the extension archive.
38
39
Returns:
40
A dictionary containing the extension ID.
41
"""
42
if sum(x is not None for x in (path, archive_path, base64_value)) != 1:
43
raise ValueError("Exactly one of path, archive_path, or base64_value must be provided")
44
45
if path is not None:
46
extension_data = {"type": "path", "path": path}
47
elif archive_path is not None:
48
extension_data = {"type": "archivePath", "path": archive_path}
49
elif base64_value is not None:
50
extension_data = {"type": "base64", "value": base64_value}
51
52
params = {"extensionData": extension_data}
53
54
try:
55
result = self.conn.execute(command_builder("webExtension.install", params))
56
return result
57
except WebDriverException as e:
58
if "Method not available" in str(e):
59
raise WebDriverException(
60
f"{e!s}. If you are using Chrome or Edge, add '--enable-unsafe-extension-debugging' "
61
"and '--remote-debugging-pipe' arguments or set options.enable_webextensions = True"
62
) from e
63
raise
64
65
def uninstall(self, extension_id_or_result: str | dict) -> None:
66
"""Uninstalls a web extension from the remote end.
67
68
Args:
69
extension_id_or_result: Either the extension ID as a string or the result dictionary
70
from a previous install() call containing the extension ID.
71
"""
72
if isinstance(extension_id_or_result, dict):
73
extension_id = extension_id_or_result.get("extension")
74
else:
75
extension_id = extension_id_or_result
76
77
params = {"extension": extension_id}
78
self.conn.execute(command_builder("webExtension.uninstall", params))
79
80