Path: blob/trunk/py/selenium/webdriver/common/bidi/webextension.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.1617from typing import Union1819from selenium.common.exceptions import WebDriverException20from selenium.webdriver.common.bidi.common import command_builder212223class WebExtension:24"""25BiDi implementation of the webExtension module.26"""2728def __init__(self, conn):29self.conn = conn3031def install(self, path=None, archive_path=None, base64_value=None) -> dict:32"""Installs a web extension in the remote end.3334You must provide exactly one of the parameters.3536Parameters:37-----------38path: Path to an extension directory39archive_path: Path to an extension archive file40base64_value: Base64 encoded string of the extension archive4142Returns:43-------44Dict: A dictionary containing the extension ID.45"""46if sum(x is not None for x in (path, archive_path, base64_value)) != 1:47raise ValueError("Exactly one of path, archive_path, or base64_value must be provided")4849if path is not None:50extension_data = {"type": "path", "path": path}51elif archive_path is not None:52extension_data = {"type": "archivePath", "path": archive_path}53elif base64_value is not None:54extension_data = {"type": "base64", "value": base64_value}5556params = {"extensionData": extension_data}5758try:59result = self.conn.execute(command_builder("webExtension.install", params))60return result61except WebDriverException as e:62if "Method not available" in str(e):63raise WebDriverException(64f"{str(e)}. If you are using Chrome or Edge, add '--enable-unsafe-extension-debugging' "65"and '--remote-debugging-pipe' arguments or set options.enable_webextensions = True"66) from e67raise6869def uninstall(self, extension_id_or_result: Union[str, dict]) -> None:70"""Uninstalls a web extension from the remote end.7172Parameters:73-----------74extension_id_or_result: Either the extension ID as a string or the result dictionary75from a previous install() call containing the extension ID.76"""77if isinstance(extension_id_or_result, dict):78extension_id = extension_id_or_result.get("extension")79else:80extension_id = extension_id_or_result8182params = {"extension": extension_id}83self.conn.execute(command_builder("webExtension.uninstall", params))848586