如何通过Selenium在Edge浏览器中加载扩展(含APPX格式)?
Hey there! I totally get the confusion—moving from Chrome's CRX extensions to Edge's APPX format feels like a whole different ballgame, especially since Edge doesn’t have a direct addExtensions method like Chrome. Let’s break this down step by step to get your APPX extension loaded successfully.
Step 1: Install the APPX Extension Locally
First, you need to install the APPX file onto your system. You can do this easily via PowerShell:
Add-AppxPackage -Path "C:\full\path\to\your\extension-file.appx"
Replace the path with the actual location of your APPX file. If the extension has dependencies (like .appxbundle or .cer files), install those first to avoid errors.
Step 2: Locate the Extension’s Key Details
Once installed, grab two critical pieces of info:
- Extension ID: Open Edge, go to
edge://extensions/, toggle on Developer mode (top-right corner), then copy the ID listed under your APPX extension. - Installation Path: Open File Explorer and navigate to:
To findC:\Users\<YourUsername>\AppData\Local\Packages\<ExtensionPackageName>\LocalState\extension<ExtensionPackageName>, run this PowerShell command (replaceYourExtensionNamewith a keyword from your extension):Get-AppxPackage | Where-Object {$_.Name -match "YourExtensionName"} | Select-Object PackageFullName
Step 3: Configure Selenium to Load the Extension
Instead of an addExtensions method, we’ll use Edge’s command-line arguments to load the extension. Here’s a Python example:
from selenium import webdriver from selenium.webdriver.edge.options import Options edge_options = Options() # Option 1: Use the full installation path edge_options.add_argument(f"--load-extension=C:\\Users\\<YourUsername>\\AppData\\Local\\Packages\\<ExtensionPackageName>\\LocalState\\extension") # Option 2: Use the extension ID (works for some setups) # edge_options.add_argument(f"--load-extension=<YourExtensionID>") # Launch Edge with the configured options driver = webdriver.Edge(options=edge_options) driver.get("https://www.example.com")
Key Tips to Avoid Headaches
- Keep Developer Mode Enabled: Edge requires this to load custom/unpacked extensions like your APPX file.
- Match WebDriver Version: Ensure your Edge WebDriver matches your installed Edge browser version—mismatches cause weird bugs.
- Permissions: If your extension needs specific permissions, you might have to accept them manually on first launch (auto-accepting permissions varies by extension type).
内容的提问来源于stack exchange,提问作者Nili Karavani

