python - 无法在 Python 中连接到 docker 容器内的 Selenium
问题描述
我正在尝试在 Python 中连接 docker 容器内的 selenium。
我正在使用M1 mac。我采取的步骤是
$ docker run -d -p 4444:4444 --shm-size="2g" selenium/standalone-chrome:4.0.0-rc-1-prerelease-20210804
(此命令引用自https://github.com/SeleniumHQ/docker-selenium)- 执行下面的 Python 代码。
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
driver = webdriver.Remote(
command_executor="http://localhost:4444/wd/hub",
desired_capabilities=DesiredCapabilities.CHROME
)
driver.get("https://stackoverflow.com/")
print(driver.title)
driver.quit()
它给了我下面显示的错误。
Traceback (most recent call last):
File "/Users/ren/Code/rarejob-tutor-checker/server/index.py", line 9, in <module>
driver = webdriver.Remote(
File "/Users/ren/.local/share/virtualenvs/rarejob-tutor-checker-yZ3a8Uiu/lib/python3.9/site-packages/selenium/webdriver/remote/webdriver.py", line 157, in __init__
self.start_session(capabilities, browser_profile)
File "/Users/ren/.local/share/virtualenvs/rarejob-tutor-checker-yZ3a8Uiu/lib/python3.9/site-packages/selenium/webdriver/remote/webdriver.py", line 252, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File "/Users/ren/.local/share/virtualenvs/rarejob-tutor-checker-yZ3a8Uiu/lib/python3.9/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "/Users/ren/.local/share/virtualenvs/rarejob-tutor-checker-yZ3a8Uiu/lib/python3.9/site-packages/selenium/webdriver/remote/errorhandler.py", line 130, in check_response
status = value["status"]
KeyError: 'status'
我不知道为什么会这样。
※ 笔记
- 我可以连接到
http://localhost:4444/ui/index.html#/
屏幕截图 curl
也有效
$ curl http://localhost:4444/status
{
"value": {
"ready": true,
"message": "Selenium Grid ready.",
"nodes": [
{
"id": "af7033c1-aea3-46f3-9669-e8b1edb4922a",
"uri": "http:\u002f\u002f4d3bede65cd7:4444",
"maxSessions": 1,
"osInfo": {
"arch": "amd64",
"name": "Linux",
"version": "5.10.25-linuxkit"
},
"heartbeatPeriod": 60000,
"availability": "UP",
"version": "4.0.0-rc-1 (revision c498dad8c5)",
"slots": [
{
"lastStarted": "2021-08-09T23:37:31.804771Z",
"session": null,
"id": {
"hostId": "af7033c1-aea3-46f3-9669-e8b1edb4922a",
"id": "eec094e6-ac6d-4fbc-907c-56d7d4cf8090"
},
"stereotype": {
"browserName": "chrome",
"browserVersion": "92.0",
"platformName": "Linux",
"se:vncEnabled": true
}
}
]
}
]
}
}
解决方案
似乎 Selenium 官方映像selenium/standalone-chrome
在 M1 Mac OS 上不起作用,因为代码在 Ubuntu OS 上运行时可以正常工作。
推荐阅读
- ios - 如何从详细视图控制器中删除数据并弹回上一个表视图控制器
- linux - 为 UNIX 编写了一个 bash 代码以充当回收站。坚持将参数传递给测试函数
- ruby-on-rails - 加载路由后运行初始化代码
- java - 在 Java 中编辑和更新 ArrayList
- php - 添加新列时如何合并所有产品表?
- excel - SpecFlow+ Excel 以编程方式生成特征文件
- javascript - 动画的帧重复?
- youtube - 如何阻止烦人的“登录 YouTube”弹出窗口?
- html - CSS HTML 列表项类
- google-pagespeed - 看到 Lighthouse 和 Page Speed Insights 之间的 30–40 点差异