如何在WebDriver中运行Manifest V3版本的AdGuard扩展?
问题:适配Manifest V3版本AdGuard Chrome扩展的Selenium代码修改
原运行代码
from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.chrome.service import Service from selenium.webdriver.chrome.options import Options from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC import time import os # Create a new profile folder in the current script directory profile_dir = os.path.abspath("selenium_profile") os.makedirs(profile_dir, exist_ok=True) # URL stored as a string url = "https://example.com" # Insert the actual URL here adguard_extension_path = r"C:/Users/USER_NAME/AppData/Local/Google/Chrome/User Data/Default/Extensions/bgnkhhnnamicmpeenaelnjfhikgbkllg/5.2.80_0" # Set Chrome options (modify as needed) options = Options() options.add_argument("--start-maximized") options.add_argument(f"--user-data-dir={profile_dir}") options.add_argument(f"--load-extension={adguard_extension_path}") options.add_argument(f"--disable-extensions-except={adguard_extension_path}") # options.add_argument("--headless") # Use this to run browser in headless mode # Set driver path (modify if needed) # service = Service("/path/to/chromedriver") # Set chromedriver path # Launch the driver driver = webdriver.Chrome(options=options) try: # Open the URL driver.get(url) # Wait until the button loads (up to 10 seconds) wait = WebDriverWait(driver, 10) button = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.btn.drop-btn.second"))) # Click the button button.click() print("Button clicked successfully!") except Exception as e: print(f"Error occurred: {e}") finally: print(driver.capabilities.get("chrome", {})) time.sleep(3) # Wait to observe the result (optional) driver.quit()
原代码运行输出
Button clicked successfully! {'chromedriverVersion': '141.0.7390.76 (d6fcfbb51aac4df61d06692d4a62fd8b9aaf3c3a-refs/branch-heads/7390_65@{#3})', 'userDataDir': 'C:\Users\USER_NAME\OneDrive\Documents\Python\sel\selenium_profile'} [Finished in 11.7s]
需求说明
需要将上述代码适配Manifest V3版本的AdGuard Chrome扩展(扩展路径:C:\Users\USER_NAME\AppData\Local\Google\Chrome\User Data\Default\Extensions\bgnkhhnnamicmpeenaelnjfhikgbkllg\5.2.80_0),请问该如何修改?
解决方案
针对Manifest V3扩展的特性,需要对原代码做以下关键修改:
- 移除冲突参数:Manifest V3扩展不兼容
--disable-extensions-except参数,该参数会导致扩展加载失败,直接删除该行即可。 - 添加V3支持参数:新增
--enable-features=ExtensionManifestV3参数,确保Chrome启用对Manifest V3扩展的支持(部分新版本Chrome默认已开启,显式添加可避免版本兼容问题)。 - 验证扩展路径:确认指定的扩展目录内
manifest.json文件中"manifest_version"字段值为3,确保加载的是V3版本扩展。
修改后的完整代码
from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.chrome.service import Service from selenium.webdriver.chrome.options import Options from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC import time import os # 创建当前脚本目录下的新配置文件夹 profile_dir = os.path.abspath("selenium_profile") os.makedirs(profile_dir, exist_ok=True) # 目标URL url = "https://example.com" # 替换为实际访问的URL # Manifest V3版AdGuard扩展路径 adguard_extension_path = r"C:/Users/USER_NAME/AppData/Local/Google/Chrome/User Data/Default/Extensions/bgnkhhnnamicmpeenaelnjfhikgbkllg/5.2.80_0" # 配置Chrome选项 options = Options() options.add_argument("--start-maximized") options.add_argument(f"--user-data-dir={profile_dir}") options.add_argument(f"--load-extension={adguard_extension_path}") # 启用Manifest V3支持 options.add_argument("--enable-features=ExtensionManifestV3") # 若使用无头模式,建议用新版参数,旧版对V3扩展支持不佳 # options.add_argument("--headless=new") # 设置驱动路径(按需修改) # service = Service("/path/to/chromedriver") # 启动浏览器驱动 driver = webdriver.Chrome(options=options) try: # 打开目标URL driver.get(url) # 等待按钮加载完成(最长10秒) wait = WebDriverWait(driver, 10) button = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.btn.drop-btn.second"))) # 点击按钮 button.click() print("按钮点击成功!") except Exception as e: print(f"发生错误:{e}") finally: print(driver.capabilities.get("chrome", {})) time.sleep(3) # 观察结果(可选) driver.quit()
额外注意事项
- 若使用无头模式,必须改用
--headless=new参数,旧版--headless参数对Manifest V3扩展存在兼容性问题。 - 运行前务必检查扩展目录下的
manifest.json,确认manifest_version为3,避免加载到旧版V2扩展。
内容的提问来源于stack exchange,提问作者Luke
相关产品推荐
相关产品推荐

