You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python Selenium加载AntiCaptcha扩展报错:无法读取清单文件

问题描述

正在执行网页爬取任务,目标网站采用reCAPTCHA v3(一款后台运行、无需用户交互的自动化访问拦截工具),尝试通过加载AntiCaptcha扩展绕过验证,但运行代码时出现以下错误:

Message: session not created: cannot process extension #1 from unknown error: cannot read manifest

项目结构:
项目结构

使用的Python代码:

def start_driver():
    try:
        
        url = 'https://antcpt.com/anticaptcha-plugin.zip'
        filehandle, _ = urllib.request.urlretrieve(url)
        with zipfile.ZipFile(filehandle, "r") as f:
            f.extractall("plugin")
        api_key = "MY API KEY"
        file = Path('./plugin/js/config_ac_api_key.js')
        file.write_text(file.read_text().replace("antiCapthaPredefinedApiKey = ''", "antiCapthaPredefinedApiKey = '{}'".format(api_key)))

        zip_file = zipfile.ZipFile('./plugin.zip', 'w', zipfile.ZIP_DEFLATED)
        for root, dirs, files in os.walk("./plugin"):
                for file in files:
                    path = os.path.join(root, file)
                    zip_file.write(path, arcname=path.replace("./plugin/", ""))
        zip_file.close()

        chrome_options = webdriver.ChromeOptions()
        chrome_options.add_extension('./plugin.zip')
        chrome_options.add_argument("--start-maximized")
        chrome_options.add_argument("--no-sandbox")
        chrome_options.add_argument("--disable-dev-shm-usage")
        driver = webdriver.Chrome(options=chrome_options)
        return driver
    except Exception as e:
        print("Error al iniciar el controlador:", e)
        return None

if __name__ == '__main__':
    driver = start_driver()
解决方案

该错误核心原因是重新打包的扩展文件结构异常,导致Chrome无法读取manifest.json文件。可通过以下步骤修复:

1. 修正扩展打包逻辑

原打包代码可能因路径处理问题打乱了扩展的目录结构,调整为更严谨的相对路径处理:

# 替换原打包代码段
zip_file = zipfile.ZipFile('./plugin.zip', 'w', zipfile.ZIP_DEFLATED)
base_dir = "./plugin"
for root, dirs, files in os.walk(base_dir):
    rel_path = os.path.relpath(root, base_dir)
    for file in files:
        file_path = os.path.join(root, file)
        arcname = file if rel_path == "." else os.path.join(rel_path, file)
        zip_file.write(file_path, arcname=arcname)
zip_file.close()

2. 检查扩展文件完整性

手动解压下载的anticaptcha-plugin.zip,确认manifest.json直接位于plugin根目录下,且文件本身无语法错误(如缺失逗号、引号不匹配)。

3. 尝试无压缩打包模式

部分Chrome版本对压缩后的扩展兼容性较差,改用无压缩模式打包:

zip_file = zipfile.ZipFile('./plugin.zip', 'w', zipfile.ZIP_STORED)

4. 直接加载解压后的扩展目录(快速测试)

跳过打包环节,让Chrome直接读取解压后的目录,验证扩展本身是否正常:

# 替换原add_extension代码行
chrome_options.add_argument(f"--load-extension={os.path.abspath('./plugin')}")

额外注意事项

  • 确保Chrome浏览器与ChromeDriver版本严格匹配,版本不兼容也可能引发扩展加载失败。
  • 确认config_ac_api_key.js中的API密钥已正确替换,无语法错误。

内容的提问来源于stack exchange,提问作者Luis Alva Celis

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.29 02:37:38