docker - Python-Selenium Remote Connection to Dockerized Selenium Server - Stack Overflow

I'm trying to connect to a Dockerized version of Selenium on the same host within one Python scrip

I'm trying to connect to a Dockerized version of Selenium on the same host within one Python script. This is running on a server, so this is necessarily headless.

I'd like to use the Firefox driver. The Docker container seems to be created and runs fine, however I keep getting "the connection reset by peer error".

My Python script is as follows:

import docker
from selenium import webdriver

client = docker.from_env()

client.images.pull("selenium/standalone-firefox:135.0.1-geckodriver-0.36.0-20250303")

firefox = client.containers.run("selenium/standalone-firefox:135.0.1-geckodriver-0.36.0-20250303",
                                detach = True,
                                name = "firefox",
                                ports = {4444: 4444, 7900: 7900},
                                shm_size = "2G",
                                environment = ["SE_START_XVFB=false",
                                               "SE_SCREEN_WIDTH=1200",
                                               "SE_SCREEN_HEIGHT=900"])

print(client.containers.list()) # this shows me the container, also I see it in `docker ps`

try:

    driver = webdriver.Remote(
        command_executor="http://127.0.0.1:4444/wd/hub",
        options=webdriver.FirefoxOptions()
    )
    
    driver.get(";)

except Exception as e:
    
    print("An exception occurred: ", e)

This raises the exception:

An exception occurred: ('Connection aborted.', ConnectionResetError(104, 'Connection reset by peer'))

How do I resolve this?

I'm trying to connect to a Dockerized version of Selenium on the same host within one Python script. This is running on a server, so this is necessarily headless.

I'd like to use the Firefox driver. The Docker container seems to be created and runs fine, however I keep getting "the connection reset by peer error".

My Python script is as follows:

import docker
from selenium import webdriver

client = docker.from_env()

client.images.pull("selenium/standalone-firefox:135.0.1-geckodriver-0.36.0-20250303")

firefox = client.containers.run("selenium/standalone-firefox:135.0.1-geckodriver-0.36.0-20250303",
                                detach = True,
                                name = "firefox",
                                ports = {4444: 4444, 7900: 7900},
                                shm_size = "2G",
                                environment = ["SE_START_XVFB=false",
                                               "SE_SCREEN_WIDTH=1200",
                                               "SE_SCREEN_HEIGHT=900"])

print(client.containers.list()) # this shows me the container, also I see it in `docker ps`

try:

    driver = webdriver.Remote(
        command_executor="http://127.0.0.1:4444/wd/hub",
        options=webdriver.FirefoxOptions()
    )
    
    driver.get("http://www.python.")

except Exception as e:
    
    print("An exception occurred: ", e)

This raises the exception:

An exception occurred: ('Connection aborted.', ConnectionResetError(104, 'Connection reset by peer'))

How do I resolve this?

Share Improve this question asked Mar 12 at 3:01 GNUserGNUser 1,6686 gold badges33 silver badges52 bronze badges 1
  • did you try to run without try/except to get more details in error message? – furas Commented Mar 12 at 8:53
Add a comment  | 

1 Answer 1

Reset to default 1
  1. The Selenium server inside the Docker container might take a few seconds to start up completely. Added time.sleep(10)
  2. In server need to enable headless mode to run.

Working code:-

import time

import docker
from selenium import webdriver

client = docker.from_env()

client.images.pull("selenium/standalone-firefox:135.0.1-geckodriver-0.36.0-20250303")

firefox = client.containers.run("selenium/standalone-firefox:135.0.1-geckodriver-0.36.0-20250303",
                                detach = True,
                                name = "firefox",
                                ports = {4444: 4444, 7900: 7900},
                                shm_size = "2G",
                                environment = ["SE_START_XVFB=false",
                                               "SE_SCREEN_WIDTH=1200",
                                               "SE_SCREEN_HEIGHT=900"])

print(client.containers.list())  # this shows me the container, also I see it in `docker ps`

time.sleep(10) # Wait for the Selenium server to initialize
try:

    options = webdriver.FirefoxOptions()
    options.add_argument("--headless")  # Enable headless mode
    driver = webdriver.Remote(
        command_executor="http://127.0.0.1:4444/wd/hub",
        options=options
    )

    driver.get("http://www.python.")
    print(driver.title)

except Exception as e:
    print("An exception occurred: ", e)

finally:
    # Clean up the container
    firefox.stop()
    firefox.remove()

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744771276a4592773.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信