Docker-based Grid helps run many browser tests at the same time easily. It uses containers to keep tests separate and fast.
Docker-based Grid in 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.
docker run -d -p 4444:4444 --name selenium-grid selenium/standalone-firefox
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()
This script connects to the Docker Selenium Grid with Chrome, opens example.com, prints the page title, and closes the browser.
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()
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.
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.
Practice
Solution
Step 1: Understand Docker-based Grid concept
Docker-based Grid runs browsers inside containers, isolating tests and allowing many tests to run in parallel.Step 2: Compare options with this concept
Only To run browser tests in isolated containers for better scalability correctly describes running tests in isolated containers for scalability.Final Answer:
To run browser tests in isolated containers for better scalability -> Option DQuick Check:
Docker Grid = isolated, scalable browser tests [OK]
- Thinking Docker replaces Selenium WebDriver
- Believing tests run only locally without network
- Confusing Docker Grid with test script writing
http://localhost:4444/wd/hub for Chrome browser?Solution
Step 1: Recall correct Remote WebDriver syntax
Remote WebDriver requires command_executor URL and browser options object, e.g., ChromeOptions.Step 2: Match options with correct syntax
driver = webdriver.Remote(command_executor='http://localhost:4444/wd/hub', options=webdriver.ChromeOptions()) uses webdriver.Remote with command_executor and ChromeOptions correctly; others misuse classes or parameters.Final Answer:
driver = webdriver.Remote(command_executor='http://localhost:4444/wd/hub', options=webdriver.ChromeOptions()) -> Option CQuick Check:
Remote WebDriver needs command_executor and options [OK]
- Using webdriver.Chrome instead of webdriver.Remote for grid
- Passing URL directly to webdriver.Chrome constructor
- Incorrect chaining of ChromeOptions and Remote
print(driver.title) output if the page at https://example.com has title "Example Domain"?
from selenium import webdriver
driver = webdriver.Remote(
command_executor='http://localhost:4444/wd/hub',
options=webdriver.FirefoxOptions()
)
driver.get('https://example.com')
print(driver.title)
driver.quit()Solution
Step 1: Understand code flow with Remote WebDriver
The code opens Firefox browser via Docker Grid, navigates to example.com, then prints the page title.Step 2: Recall page title for https://example.com
The official example.com page title is "Example Domain", so print(driver.title) outputs this string.Final Answer:
Example Domain -> Option AQuick Check:
driver.title = page title = "Example Domain" [OK]
- Confusing URL with page title
- Expecting None if using Remote WebDriver
- Assuming an error without browser session
from selenium import webdriver
driver = webdriver.Remote(
command_executor='http://localhost:5555/wd/hub',
options=webdriver.ChromeOptions()
)
driver.get('https://example.com')Solution
Step 1: Analyze connection error cause
Connection errors usually mean the Selenium Grid server is unreachable at the given URL and port.Step 2: Check port and server status
Port 5555 is uncommon; default Docker Grid uses 4444. If Grid is not running on 5555, connection fails.Final Answer:
Docker Selenium Grid is not running on port 5555 -> Option AQuick Check:
Connection error = wrong port or server down [OK]
- Thinking ChromeOptions can't be used remotely
- Blaming website URL for connection errors
- Assuming Remote needs local browser installed
Solution
Step 1: Understand Docker Compose setup for Selenium Grid
The hub service exposes port 4444. Each node (chrome, firefox) must depend on hub and set environment variables for event bus host and ports.Step 2: Check environment variables and dependencies
version: '3' services: selenium-hub: image: selenium/hub:4.10.0-20230628 ports: - '4444:4444' chrome: image: selenium/node-chrome:4.10.0-20230628 depends_on: - selenium-hub environment: - SE_EVENT_BUS_HOST=selenium-hub - SE_EVENT_BUS_PUBLISH_PORT=4442 - SE_EVENT_BUS_SUBSCRIBE_PORT=4443 firefox: image: selenium/node-firefox:4.10.0-20230628 depends_on: - selenium-hub environment: - SE_EVENT_BUS_HOST=selenium-hub - SE_EVENT_BUS_PUBLISH_PORT=4442 - SE_EVENT_BUS_SUBSCRIBE_PORT=4443 correctly sets SE_EVENT_BUS_HOST, SE_EVENT_BUS_PUBLISH_PORT=4442, and SE_EVENT_BUS_SUBSCRIBE_PORT=4443 for both nodes, matching Selenium Grid docs.Final Answer:
version: '3' services: selenium-hub: image: selenium/hub:4.10.0-20230628 ports: - '4444:4444' chrome: image: selenium/node-chrome:4.10.0-20230628 depends_on: - selenium-hub environment: - SE_EVENT_BUS_HOST=selenium-hub - SE_EVENT_BUS_PUBLISH_PORT=4442 - SE_EVENT_BUS_SUBSCRIBE_PORT=4443 firefox: image: selenium/node-firefox:4.10.0-20230628 depends_on: - selenium-hub environment: - SE_EVENT_BUS_HOST=selenium-hub - SE_EVENT_BUS_PUBLISH_PORT=4442 - SE_EVENT_BUS_SUBSCRIBE_PORT=4443 -> Option BQuick Check:
Correct event bus ports and depends_on = version: '3' services: selenium-hub: image: selenium/hub:4.10.0-20230628 ports: - '4444:4444' chrome: image: selenium/node-chrome:4.10.0-20230628 depends_on: - selenium-hub environment: - SE_EVENT_BUS_HOST=selenium-hub - SE_EVENT_BUS_PUBLISH_PORT=4442 - SE_EVENT_BUS_SUBSCRIBE_PORT=4443 firefox: image: selenium/node-firefox:4.10.0-20230628 depends_on: - selenium-hub environment: - SE_EVENT_BUS_HOST=selenium-hub - SE_EVENT_BUS_PUBLISH_PORT=4442 - SE_EVENT_BUS_SUBSCRIBE_PORT=4443 [OK]
- Omitting depends_on causing nodes to start before hub
- Wrong or missing event bus port environment variables
- Swapping publish and subscribe port numbers
Ready to prove it?
Pass the test and this pattern moves unknown
