admin管理员组文章数量:1391749
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 |1 Answer
Reset to default 1- The Selenium server inside the Docker container might take a few seconds to start up completely. Added
time.sleep(10)
- 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()
本文标签: dockerPythonSelenium Remote Connection to Dockerized Selenium ServerStack Overflow
版权声明:本文标题:docker - Python-Selenium Remote Connection to Dockerized Selenium Server - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744771261a2624353.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
try/except
to get more details in error message? – furas Commented Mar 12 at 8:53