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

Selenium报错:无法实例化抽象类Service,缺少command_line_args实现

解决Selenium中TypeError: Can't instantiate abstract class Service的问题

这个错误的核心原因是直接实例化了Selenium的抽象基类Service,而非对应浏览器的具体Service实现类(比如ChromeService、FirefoxService)。抽象类本身不能被直接实例化,必须使用它的子类。

修复步骤:

  • 导入对应浏览器的Service子类,而非抽象的Service基类:
    比如使用Chrome浏览器时,导入路径应为:

    from selenium.webdriver.chrome.service import Service
    

    而非:

    from selenium.webdriver.common.service import Service
    
  • 正确的代码示例(以Chrome为例):

    from selenium import webdriver
    from selenium.webdriver.chrome.service import Service
    
    # 初始化ChromeService,若未配置驱动环境变量,需指定executable_path
    service = Service(executable_path='chromedriver.exe')  # Windows系统
    # service = Service(executable_path='/usr/local/bin/chromedriver')  # macOS/Linux
    
    # 启动浏览器
    driver = webdriver.Chrome(service=service)
    
    # 后续爬虫操作示例
    driver.get("https://www.example.com")
    # ... 你的爬虫逻辑 ...
    
    # 关闭浏览器
    driver.quit()
    

额外注意:

确保你的Selenium版本与浏览器驱动版本完全匹配(比如Chrome浏览器对应ChromeDriver版本),版本不匹配也可能引发各类异常。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.23 10:22:32