M1 Mac下Python无法连接Docker内Selenium 报错KeyError: 'status'
问题原因
- 版本协议不兼容:你部署的Selenium Grid 4.x使用W3C标准响应格式,而本地Python Selenium库为3.x版本,仅支持旧版JSONWP协议,解析响应时无法匹配结构导致
KeyError: 'status' - 架构兼容性问题:你拉取的
selenium/standalone-chrome镜像为amd64架构,在M1 arm64架构设备上通过Docker转译运行,存在潜在兼容隐患
解决步骤
升级本地Python Selenium库到4.x版本
执行命令:pip install --upgrade selenium调整Python连接代码,适配Selenium 4的W3C协议规范
代码示例:
from selenium import webdriver from selenium.webdriver.chrome.options import Options options = Options() driver = webdriver.Remote( command_executor="http://localhost:4444/wd/hub", options=options ) driver.get("https://stackoverflow.com/") print(driver.title) driver.quit()
- (M1设备可选优化)更换为arm64架构适配的Selenium镜像
停止原有容器后,使用如下命令启动服务:docker run -d -p 4444:4444 --shm-size="2g" seleniarm/standalone-chromium:latest
调整完成后重新运行代码即可正常执行。
内容的提问来源于stack exchange,提问作者Ren
相关产品推荐
相关产品推荐

