使用Selenium+Chrome报错:chromedriver意外退出(状态码-9)求助
解决ChromeDriver意外退出(状态码-9)的问题
你的代码
service = Service(executable_path=ChromeDriverManager().install()) options1 = Options() options1.headless = True driver1 = webdriver.Chrome(service=service, options=options1) driver1.get("https://google.com")
报错信息
selenium.common.exceptions.WebDriverException: Message: Service my path/chromedriver unexpectedly exited. Status code was: -9
可行解决方案
强制匹配Chrome与ChromeDriver版本
状态码-9大概率是版本不兼容导致,即便用webdriver-manager也可能出现检测偏差。手动处理:- 打开Chrome输入
chrome://version/查看浏览器版本 - 下载对应大版本的ChromeDriver,替换webdriver-manager下载的驱动,或直接指定本地驱动路径初始化Service
- 打开Chrome输入
更新Headless模式配置
旧版options.headless = True在新版Chrome中存在兼容性问题,替换为:options1.add_argument("--headless=new")同时补充参数避免资源/权限问题:
options1.add_argument("--disable-gpu") options1.add_argument("--no-sandbox") options1.add_argument("--disable-dev-shm-usage")检查系统资源与权限
- 确保运行环境有足够内存,内存不足会导致ChromeDriver意外退出
- Linux/macOS系统给ChromeDriver添加可执行权限:
chmod +x /path/to/chromedriver
清除webdriver-manager缓存
删除缓存目录后重新运行,避免旧驱动干扰:- macOS/Linux:
~/.wdm - Windows:
C:\Users\<你的用户名>\.wdm
- macOS/Linux:
内容的提问来源于stack exchange,提问作者vate
相关产品推荐
相关产品推荐

