Bird
Raised Fist0
Selenium Pythontesting~10 mins

Docker-based Grid in Selenium Python - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to start a remote WebDriver session using Selenium Grid.

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.[1]
)
Drag options to blanks, or click blank then click option'
ASAFARI
BFIREFOX
CEDGE
DCHROME
Attempts:
3 left
💡 Hint
Common Mistakes
Using a browser name that is not supported or misspelled.
Confusing the capability constants with lowercase strings.
2fill in blank
medium

Complete the code to specify the Selenium Grid hub URL correctly.

Selenium Python
hub_url = 'http://[1]:4444/wd/hub'
driver = webdriver.Remote(
    command_executor=hub_url,
    desired_capabilities=DesiredCapabilities.CHROME
)
Drag options to blanks, or click blank then click option'
A127.0.0.1
Blocalhost
Cselenium-hub
D192.168.1.100
Attempts:
3 left
💡 Hint
Common Mistakes
Using an IP address that does not match the hub's actual address.
Leaving the URL incomplete or missing the port.
3fill in blank
hard

Fix the error in the code to correctly import the Selenium WebDriver module for remote execution.

Selenium Python
from selenium import [1]
driver = webdriver.Remote(
    command_executor='http://localhost:4444/wd/hub',
    desired_capabilities=DesiredCapabilities.CHROME
)
Drag options to blanks, or click blank then click option'
Aremote
Bwebdriver_remote
Cwebdriver
Dselenium
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to import a non-existent module like remote or webdriver_remote.
Importing the whole selenium package instead of the webdriver submodule.
4fill in blank
hard

Fill both blanks to create a dictionary comprehension that filters nodes with status 'ready' and extracts their names.

Selenium Python
ready_nodes = {node['name']: node for node in nodes if node[1]'status' == '[2]'}
Drag options to blanks, or click blank then click option'
A['
C.get(
Attempts:
3 left
💡 Hint
Common Mistakes
Using dot notation instead of square brackets for dictionary keys.
Forgetting quotes around the key name.
5fill in blank
hard

Fill all three blanks to create a test assertion that checks if the page title contains the word 'Dashboard'.

Selenium Python
assert '[1]' in driver.title, 'Page title does not contain [2]'
print('Test [3]')
Drag options to blanks, or click blank then click option'
ADashboard
Cpassed
Dfailed
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong word in the assertion or print statement.
Mixing up the assertion message and the print output.

Practice

(1/5)
1. What is the main purpose of using a Docker-based Selenium Grid?
easy
A. To run tests only on the local machine without network
B. To write test scripts without any browser
C. To replace Selenium WebDriver with a new tool
D. To run browser tests in isolated containers for better scalability

Solution

  1. Step 1: Understand Docker-based Grid concept

    Docker-based Grid runs browsers inside containers, isolating tests and allowing many tests to run in parallel.
  2. 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.
  3. Final Answer:

    To run browser tests in isolated containers for better scalability -> Option D
  4. Quick Check:

    Docker Grid = isolated, scalable browser tests [OK]
Hint: Docker Grid isolates browsers to run many tests fast [OK]
Common Mistakes:
  • Thinking Docker replaces Selenium WebDriver
  • Believing tests run only locally without network
  • Confusing Docker Grid with test script writing
2. Which Python code snippet correctly creates a Remote WebDriver session to connect to a Docker-based Selenium Grid at URL http://localhost:4444/wd/hub for Chrome browser?
easy
A. driver = webdriver.Remote('http://localhost:4444/wd/hub', browser='chrome')
B. driver = webdriver.Chrome('http://localhost:4444/wd/hub')
C. driver = webdriver.Remote(command_executor='http://localhost:4444/wd/hub', options=webdriver.ChromeOptions())
D. driver = webdriver.ChromeOptions().Remote('http://localhost:4444/wd/hub')

Solution

  1. Step 1: Recall correct Remote WebDriver syntax

    Remote WebDriver requires command_executor URL and browser options object, e.g., ChromeOptions.
  2. 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.
  3. Final Answer:

    driver = webdriver.Remote(command_executor='http://localhost:4444/wd/hub', options=webdriver.ChromeOptions()) -> Option C
  4. Quick Check:

    Remote WebDriver needs command_executor and options [OK]
Hint: Use webdriver.Remote with command_executor and options [OK]
Common Mistakes:
  • Using webdriver.Chrome instead of webdriver.Remote for grid
  • Passing URL directly to webdriver.Chrome constructor
  • Incorrect chaining of ChromeOptions and Remote
3. Given this Python code connecting to a Docker Selenium Grid, what will 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()
medium
A. Example Domain
B. https://example.com
C. None
D. Error: NoSuchWindowException

Solution

  1. 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.
  2. 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.
  3. Final Answer:

    Example Domain -> Option A
  4. Quick Check:

    driver.title = page title = "Example Domain" [OK]
Hint: Page title prints exactly what the website sets [OK]
Common Mistakes:
  • Confusing URL with page title
  • Expecting None if using Remote WebDriver
  • Assuming an error without browser session
4. You run this Python test script with Docker Selenium Grid but get a connection error. What is the likely cause?
from selenium import webdriver

driver = webdriver.Remote(
    command_executor='http://localhost:5555/wd/hub',
    options=webdriver.ChromeOptions()
)
driver.get('https://example.com')
medium
A. Docker Selenium Grid is not running on port 5555
B. ChromeOptions cannot be used with Remote WebDriver
C. The URL 'https://example.com' is invalid
D. webdriver.Remote requires a local Chrome browser

Solution

  1. Step 1: Analyze connection error cause

    Connection errors usually mean the Selenium Grid server is unreachable at the given URL and port.
  2. 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.
  3. Final Answer:

    Docker Selenium Grid is not running on port 5555 -> Option A
  4. Quick Check:

    Connection error = wrong port or server down [OK]
Hint: Check Docker Grid port matches command_executor URL [OK]
Common Mistakes:
  • Thinking ChromeOptions can't be used remotely
  • Blaming website URL for connection errors
  • Assuming Remote needs local browser installed
5. You want to run tests on Chrome and Firefox browsers simultaneously using Docker Selenium Grid. Which Docker Compose service setup is correct to start both browser nodes connected to the hub?
hard
A. version: '3' services: selenium-hub: image: selenium/hub:4.10.0-20230628 ports: - '4444:4444' chrome: image: selenium/node-chrome:4.10.0-20230628 environment: - SE_EVENT_BUS_HOST=selenium-hub firefox: image: selenium/node-firefox:4.10.0-20230628 environment: - SE_EVENT_BUS_HOST=selenium-hub
B. 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
C. 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 firefox: image: selenium/node-firefox:4.10.0-20230628 depends_on: - selenium-hub
D. 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=4443 - SE_EVENT_BUS_SUBSCRIBE_PORT=4442 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

Solution

  1. 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.
  2. 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.
  3. 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 B
  4. Quick 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]
Hint: Set SE_EVENT_BUS_HOST and ports exactly as docs show [OK]
Common Mistakes:
  • Omitting depends_on causing nodes to start before hub
  • Wrong or missing event bus port environment variables
  • Swapping publish and subscribe port numbers
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard