0
0
Selenium Pythontesting~5 mins

Docker-based Grid in Selenium Python

Choose your learning style9 modes available
Introduction

Docker-based Grid helps run many browser tests at the same time easily. It uses containers to keep tests separate and fast.

You want to run tests on different browsers without installing them all on your computer.
You need to run many tests at once to save time.
You want to share the same test setup with your team easily.
You want to avoid conflicts between different browser versions.
You want to run tests on a clean environment every time.
Syntax
Selenium Python
docker run -d -p 4444:4444 --name selenium-grid selenium/standalone-chrome

# In Python test script:
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://example.com')
print(driver.title)
driver.quit()

The first command starts a Docker container with a Chrome browser ready for testing.

The Python code connects to this container to run browser commands remotely.

Examples
This starts a Docker container with Firefox browser for testing.
Selenium Python
docker run -d -p 4444:4444 --name selenium-grid selenium/standalone-firefox
This Python code connects to the Firefox Docker container to run tests.
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.FIREFOX
)
driver.get('https://example.com')
print(driver.title)
driver.quit()
Sample Program

This script connects to the Docker Selenium Grid with Chrome, opens example.com, prints the page title, and closes the browser.

Selenium Python
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

# Connect to Docker-based Selenium Grid running Chrome

driver = webdriver.Remote(
    command_executor='http://localhost:4444/wd/hub',
    desired_capabilities=DesiredCapabilities.CHROME
)

# Open a website
driver.get('https://example.com')

# Print the page title
print(driver.title)

# Close the browser
driver.quit()
OutputSuccess
Important Notes

Make sure Docker is installed and running before starting the Selenium Grid container.

Use the correct port (default 4444) and URL when connecting from your test script.

Always quit the driver to free resources after the test finishes.

Summary

Docker-based Grid lets you run browser tests in isolated containers.

You connect your test scripts to the grid using Remote WebDriver.

This setup helps run many tests faster and keeps your computer clean.