Python3+selenium配置常见报错解决方案

星野
星野
发布时间:2026-10-14| 5人浏览过
原创

本文详细解析Python3结合Selenium控制Firefox浏览器时遇到的四大常见报错。涵盖geckodriver路径配置、Firefox二进制文件环境变量设置、Marionette协议版本不兼容以及newSession会话错误。通过具体代码示例和解决方案,帮助开发者快速修复环境配置问题,确保自动化脚本稳定运行。

geckodriver 驱动未配置到 PATH 路径

第一个常见问题:系统提示找不到 'geckodriver' 可执行文件,即 'geckodriver' executable needs to be in PATH。

1. 如果在启动浏览器的过程中遇到以下错误信息:

Traceback (most recent call last): File "<stdin>", line 1, in <module> File "D:\test\python3\lib\site-packages\selenium\webdriver\firefox\webdriver.py", line 145, in __init__ self.service.start() File "D:\test\python3\lib\site-packages\selenium\webdriver\common\service.py", line 81, in start os.path.basename(self.path), self.start_error_message) selenium.common.exceptions.WebDriverException: Message: 'geckodriver' executable needs to be in PATH.

2. 该错误的原因是,最新版本的 Selenium 3.0 在启动 Firefox 浏览器时,必须依赖 geckodriver.exe 驱动文件。

3. 下载该驱动文件后,需要将其配置到系统环境变量的 PATH 路径中(也可以直接放置在 Python 的安装根目录下)。

Firefox 二进制文件路径未配置

第二个常见问题:系统提示期望找到浏览器二进制文件位置,但无法在默认位置找到,即 Expected browser binary location, but unable to find binary in default location。

1. 如果在启动浏览器的过程中遇到以下错误信息:

Traceback (most recent call last): File "<stdin>", line 1, in <module> File "D:\test\python3\lib\site-packages\selenium\webdriver\firefox\webdriver.py", line 155, in __init__ keep_alive=True) File "D:\test\python3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 92, in __init__ self.start_session(desired_capabilities, browser_profile)

File "D:\test\python3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 179, in start_session response = self.execute(Command.NEW_SESSION, capabilities) File "D:\test\python3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 238, in execute self.error_handler.check_response(response) File "D:\test\python3\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 193, in check_response raise exception_class(message, screen, stacktrace) selenium.common.exceptions.WebDriverException: Message: Expected browser binary location, but unable to find binary in default location, no 'moz:firefoxOptions.binary' capability provided, and no binary flag set on the command line.

2. 出现此错误的原因是,firefox.exe 文件也需要被配置到系统环境变量的 PATH 路径中。

3. 你需要找到安装 Firefox 后生成的 firefox.exe 文件的具体路径,并将其添加到 PATH 环境变量中。

Marionette 协议版本不兼容

第三个常见问题:提示不支持的 Marionette 协议版本 2,但要求版本为 3,即 Unsupported Marionette protocol version 2, required 3。

1. 如果在启动浏览器的过程中出现以下错误信息:

Traceback (most recent call last): File "<stdin>", line 1, in <module>

File "D:\test\python3\lib\site-packages\selenium\webdriver\firefox\webdriver.py", line 155, in __init__ keep_alive=True) File "D:\test\python3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 92, in __init__ self.start_session(desired_capabilities, browser_profile) File "D:\test\python3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 179, in start_session response = self.execute(Command.NEW_SESSION, capabilities) File "D:\test\python3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 238, in execute self.error_handler.check_response(response) File "D:\test\python3\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 193, in check_response

raise exception_class(message, screen, stacktrace) selenium.common.exceptions.WebDriverException: Message: Unsupported Marionette protocol version 2, required 3

2. 该错误的原因是当前安装的 Firefox 版本过低。最新版本的 Selenium 3.0 支持 Firefox 47 及以上的版本,因此只需升级 Firefox 浏览器版本即可解决。

newSession 会话创建失败

第四个常见问题:抛出 WebDriverException 异常,消息为 newSession。

1. 错误堆栈跟踪如下:

File “D:\test\python3\lib\site-packages\selenium\webdriver\firefox\\webdriver.py”, line 170, in init keep_alive=True) File “D:\test\python3\lib\site-packages\selenium\webdriver\firefox\\webdriver.py”, line 156, in init self.start_session(capabilities, browser_profile) File “D:\test\python3\lib\site-packages\selenium\webdriver\firefox\\webdriver.py”, line 245, in start_session response = self.execute(Command.NEW_SESSION, parameters) File “D:\test\python3\lib\site-packages\selenium\webdriver\firefox\\webdriver.py”, line 314, in execute self.error_handler.check_response(response) File “D:\test\python3\lib\site-packages\selenium\webdriver\firefox\\errorhandler.py”, line 242, in check_response raise exception_class(message, screen, stacktrace) selenium.common.exceptions.WebDriverException: Message: newSession

2. 解决方法是下载最新版本的 geckodriver.exe,并将其放置到 Python 的安装目录下。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持本站。

相关标签:

免责声明:本文内容仅供参考,不代表本站观点。转载或引用请注明出处。

Python 相关产品和应用

全部下载
Selenium

Selenium

4.51.0 · Windows

用于浏览器自动化和 Web 测试的 Python 工具。

scikit-learn

scikit-learn

1.9.1 · Windows

常用 Python 机器学习算法与模型评估工具库。

Black

Black

26.10.1 · Windows

强调一致性的 Python 自动代码格式化工具。

Ruff

Ruff

0.17.1 · macOS

由 Rust 编写的高速 Python 代码检查和格式化工具。

pytest

pytest

9.1.1 · Windows

简洁而强大的 Python 自动化测试框架。

FastAPI

FastAPI

0.146.0 · Windows

高性能 Python API 框架,支持类型提示和自动接口文档。

Flask

Flask

3.1.3 · Windows

轻量灵活的 Python Web 应用框架。

Django

Django

6.1.2 · Windows

成熟的 Python Web 框架,适合构建内容站点和业务系统。

Spyder

Spyder

6.1.7 · Windows

面向科学计算与数据分析的 Python 集成开发环境。

SciPy

SciPy

1.18.1 · Windows

面向科学、工程和技术计算的 Python 算法库。