无法连接到Python中docker容器内的Selenium

2024-05-19 03:20:37 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图在Python中连接docker容器内的selenium

我用的是M1 mac。 我采取的步骤是

  1. $ 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引用的)
  2. 执行下面的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'

我不知道为什么会这样

※注

  1. 我可以连接到http://localhost:4444/ui/index.html#/Screen shot
  2. 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
            }
          }
        ]
      }
    ]
  }
}

Tags: inpyhttpsharelocaldriverseleniumline

热门问题