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