Path: blob/trunk/py/selenium/webdriver/common/bidi/webextension.py
4065 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.161718from selenium.common.exceptions import WebDriverException19from selenium.webdriver.common.bidi.common import command_builder202122class WebExtension:23"""BiDi implementation of the webExtension module."""2425def __init__(self, conn):26self.conn = conn2728def install(self, path=None, archive_path=None, base64_value=None) -> dict:29"""Installs a web extension in the remote end.3031You must provide exactly one of the parameters.3233Args:34path: Path to an extension directory.35archive_path: Path to an extension archive file.36base64_value: Base64 encoded string of the extension archive.3738Returns:39A dictionary containing the extension ID.40"""41if sum(x is not None for x in (path, archive_path, base64_value)) != 1:42raise ValueError("Exactly one of path, archive_path, or base64_value must be provided")4344if path is not None:45extension_data = {"type": "path", "path": path}46elif archive_path is not None:47extension_data = {"type": "archivePath", "path": archive_path}48elif base64_value is not None:49extension_data = {"type": "base64", "value": base64_value}5051params = {"extensionData": extension_data}5253try:54result = self.conn.execute(command_builder("webExtension.install", params))55return result56except WebDriverException as e:57if "Method not available" in str(e):58raise WebDriverException(59f"{e!s}. If you are using Chrome or Edge, add '--enable-unsafe-extension-debugging' "60"and '--remote-debugging-pipe' arguments or set options.enable_webextensions = True"61) from e62raise6364def uninstall(self, extension_id_or_result: str | dict) -> None:65"""Uninstalls a web extension from the remote end.6667Args:68extension_id_or_result: Either the extension ID as a string or the result dictionary69from a previous install() call containing the extension ID.70"""71if isinstance(extension_id_or_result, dict):72extension_id = extension_id_or_result.get("extension")73else:74extension_id = extension_id_or_result7576params = {"extension": extension_id}77self.conn.execute(command_builder("webExtension.uninstall", params))787980