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

Selenium突然报“ModuleNotFoundError: No module named 'selenium'”的原因?

Selenium Chrome驱动无法启动排查方案

关键排查与修复步骤

  • 确认Chrome for testing版本与chromedriver版本完全匹配(需包含小版本号,比如117.0.5938.92必须对应同版本驱动)
  • 修正chromedriver路径问题:
    • 替换相对路径为绝对路径测试,例如mac/linux用/Users/xxx/chromedriver/chromedriver,Windows用C:\xxx\chromedriver.exe
    • mac/linux系统需给驱动添加执行权限:运行chmod +x chromedriver/chromedriver
  • 指定Chrome for testing的启动路径:
    新版本Selenium可能无法自动识别Chrome for testing,手动配置:
    options.binary_location = "/path/to/chrome-for-testing/chrome"  # mac/linux
    # Windows系统替换为:"C:\\path\\to\\chrome-for-testing\\chrome.exe"
    
  • 捕获详细错误日志定位问题:
    修改代码添加异常捕获,打印具体报错信息:
    from selenium import webdriver
    from selenium.webdriver.chrome.service import Service
    import traceback
    
    try:
        service = Service(executable_path='chromedriver/chromedriver')
        options = webdriver.ChromeOptions()
        options.add_argument("--verbose")  # 开启详细日志
        driver = webdriver.Chrome(service=service, options=options)
        driver.get(address)
    except Exception as e:
        print("错误详情:", str(e))
        traceback.print_exc()
    
  • 改用自动驱动管理工具(无需手动下载驱动):
    安装依赖:pip3 install webdriver-manager
    替换服务初始化代码:
    from webdriver_manager.chrome import ChromeDriverManager
    service = Service(ChromeDriverManager().install())
    
  • 检查系统权限:
    • mac/linux确认是否有chromedriver所在目录的读写权限
    • Windows排查是否被杀毒软件拦截驱动,或尝试以管理员身份运行脚本

内容的提问来源于stack exchange,提问作者g10k

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.10 12:17:03