Selenium运行报错:Service意外退出,状态码1求助
问题排查与解决方案
错误信息
selenium.common.exceptions.WebDriverException: Message: Service /pathtogeckodriver/ unexpectedly exited. Status code was: 1
代码示例
from selenium import webdriver from selenium.webdriver.firefox.service import Service s = Service(executable_path="/path/to/geckodriver") driver = webdriver.Firefox(service=s) driver.quit()
环境版本
- Python: 3.7.3
- Selenium: 4.11.2
- geckodriver: 0.23.0
1. 验证geckodriver路径正确性
- 确认
executable_path填写的是实际绝对路径,Windows系统需添加.exe后缀(如C:\tools\geckodriver.exe) - 路径中若包含空格或特殊字符,需用引号包裹或转义(如
"/path/with space/geckodriver")
2. 赋予geckodriver可执行权限(Linux/macOS)
在终端执行命令:
chmod +x /path/to/geckodriver
3. 修复版本兼容性问题
Selenium 4.11.2属于较新版本,而geckodriver 0.23.0是2018年的旧版本,二者兼容性极差:
- 升级geckodriver到与本地Firefox浏览器匹配的版本(建议选择0.33及以上版本)
- 同时确保Firefox浏览器版本与geckodriver版本对应(如Firefox 115+适配geckodriver 0.33+)
4. 指定Firefox二进制文件路径(可选)
若Firefox未安装在默认路径,需在代码中手动指定:
from selenium import webdriver from selenium.webdriver.firefox.service import Service from selenium.webdriver.firefox.options import Options s = Service(executable_path="/path/to/geckodriver") options = Options() options.binary_location = "/path/to/firefox" # Windows下为.exe路径 driver = webdriver.Firefox(service=s, options=options) driver.quit()
5. 安装系统依赖(Linux)
部分Linux发行版缺少geckodriver运行所需的依赖库,以Ubuntu为例,执行:
sudo apt-get install libgtk-3-0 libdbus-glib-1-2 libxt6
内容的提问来源于stack exchange,提问作者maic0L
相关产品推荐
相关产品推荐

