简体   繁体   中英

Can't connect to Selenium inside docker container in Python

I'm trying to connect selenium inside docker container in Python.

I'm using M1 mac. The steps I took were

  1. $ docker run -d -p 4444:4444 --shm-size="2g" selenium/standalone-chrome:4.0.0-rc-1-prerelease-20210804 (This command was referenced from https://github.com/SeleniumHQ/docker-selenium )
  2. executing Python code below.
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()

It gives me error showing below.

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'

I have no idea why it's happening.

※ Note

  1. I can connect to http://localhost:4444/ui/index.html#/ Screen shot
  2. curl also works
$ 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
            }
          }
        ]
      }
    ]
  }
}

It seems Selenium official image selenium/standalone-chrome doesn't work on M1 Mac OS because the code works properly when it's run on the Ubuntu OS.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM