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
相关产品推荐
相关产品推荐

