跳转到内容
新建笔记

Selenium:service 参数与版本匹配

现象:构造函数拒绝 service 参数。环境:原记录为 Python 3.6,完整 Selenium/浏览器/驱动版本未保留。复现:原环境未复现;下方先检查实际导入类与签名。原因:参数与导入 API 不匹配,不能仅凭升级 Python 的结果确定依赖变化。步骤:核对解释器、库签名及驱动,再按匹配版本修改。验证状态:原记录称升级后恢复;下方是2026-10-03的有限验证记录,本轮分类迁移未重新运行 Selenium 或浏览器。

TypeError: __init__() got an unexpected keyword argument 'service' 表示当前调用的构造函数不接受 service 参数。应先确认导入了哪个类、安装了哪个 Selenium 版本,以及代码与版本是否匹配。

原记录使用 Python 3.6,并通过升级到 Python 3.8 解决了当时的问题。这是旧环境的经验;解释器升级可能同时改变可安装的 Selenium 版本,不能据此认为 service 参数由 Python 3.8 提供。

先检查本机 API,不启动浏览器

跳转到“先检查本机 API,不启动浏览器”
inspect_selenium.py
import importlib.metadata as metadata
import inspect
import sys
import selenium
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
print("Python:", sys.version)
print("Executable:", sys.executable)
print("Selenium:", metadata.version("selenium"), selenium.__file__)
print("Requires-Python:", metadata.metadata("selenium").get("Requires-Python"))
signature = inspect.signature(webdriver.Chrome)
print("Chrome constructor:", signature)
if "service" not in signature.parameters:
raise RuntimeError("this imported Chrome constructor has no service parameter")
service = Service(executable_path="/path/to/chromedriver")
signature.bind(service=service, options=webdriver.ChromeOptions())
print("service argument accepted by signature; no browser was started")

/path/to/chromedriver 在这段诊断中只是占位路径:创建 Service 对象及检查签名不会启动驱动。它不能证明该路径存在,也不能证明浏览器与驱动兼容。如果连导入 Service 都失败,应保留错误并核对实际安装版本、解释器与本地同名模块。

让版本、代码和驱动对应

跳转到“让版本、代码和驱动对应”

Selenium 4 的 Chrome Python 接口接受 service=Service(...);Selenium 3 的旧构造方式不同。根据项目选择迁移代码和依赖,避免把新版本示例直接放进旧库。

在已安装相容浏览器和驱动的机器上,可使用下面的完整程序;命令行参数必须是实际驱动文件路径:

open_local_page.py
import argparse
from pathlib import Path
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
def open_page(driver_path):
driver_file = Path(driver_path).expanduser().resolve()
if not driver_file.is_file():
raise FileNotFoundError(driver_file)
service = Service(executable_path=str(driver_file))
options = webdriver.ChromeOptions()
options.add_argument("--headless=new")
driver = webdriver.Chrome(service=service, options=options)
try:
driver.get("data:text/html,<title>VitaLogos</title><p>local check</p>")
if driver.title != "VitaLogos":
raise RuntimeError("unexpected page title")
return driver.title
finally:
driver.quit()
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("driver_path")
args = parser.parse_args()
print(open_page(args.driver_path))

例如运行 python open_local_page.py /实际路径/chromedriver。Windows 使用实际的 .exe 路径。驱动需要匹配浏览器;service 关键字接受与否、驱动是否可执行、会话能否创建,是不同的检查阶段。

2026-10 核对的 Selenium 4.49 要求 Python 3.10 及以上,因此原来的“升到 3.8”不适合作为当前安装建议。以目标版本的 Requires-Python 和项目依赖约束为准,再用目标解释器安装;python -m pip show selenium 可辅助核对。

历史验证(2026-10-03)在 Python 3.11、Selenium 4.49.0 验证导入、真实构造函数签名及 Service 参数绑定,并用替身对象检查示例的路径校验、异常与 quit() 清理。没有启动浏览器、下载驱动或声称完成真实浏览器兼容测试。

依据:Selenium 4 升级指南、Python API 与版本要求。原来的 Stack Overflow 讨论 保留作问题背景。

来源:CSDN 原文,首次发布于 2024-08-28。

相关知识:Python 模块学习索引。