0
0
Selenium Pythontesting~5 mins

Grid architecture (hub and node) in Selenium Python

Choose your learning style9 modes available
Introduction

Grid architecture helps run tests on many browsers and machines at the same time. It saves time and checks if your website works everywhere.

You want to test your website on Chrome and Firefox at the same time.
You need to run tests on different computers to save time.
You want to check your website on Windows and Mac without switching machines.
You want to run many tests quickly during development.
You want to share one place to control all your test machines.
Syntax
Selenium Python
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

# Connect to the Selenium Grid Hub
driver = webdriver.Remote(
    command_executor='http://hub_ip:4444/wd/hub',
    desired_capabilities=DesiredCapabilities.CHROME
)

# Use the driver to open a website
driver.get('https://example.com')

# Close the browser
driver.quit()

The command_executor URL points to the hub where tests are sent.

DesiredCapabilities tells which browser you want to use on the node.

Examples
This connects to the hub at IP 192.168.1.10 and asks for Firefox browser on a node.
Selenium Python
driver = webdriver.Remote(
    command_executor='http://192.168.1.10:4444/wd/hub',
    desired_capabilities=DesiredCapabilities.FIREFOX
)
This connects to a local hub and requests Microsoft Edge browser.
Selenium Python
driver = webdriver.Remote(
    command_executor='http://localhost:4444/wd/hub',
    desired_capabilities=DesiredCapabilities.EDGE
)
Sample Program

This script connects to a Selenium Grid hub running locally, opens Chrome on a node, visits 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 Selenium Grid Hub
hub_url = 'http://localhost:4444/wd/hub'

try:
    driver = webdriver.Remote(
        command_executor=hub_url,
        desired_capabilities=DesiredCapabilities.CHROME
    )
    driver.get('https://example.com')
    title = driver.title
    print(f'Page title is: {title}')
finally:
    driver.quit()
OutputSuccess
Important Notes

Make sure the Selenium Grid Hub is running before starting tests.

Nodes must be registered to the hub and have the requested browsers installed.

Use IP addresses or hostnames reachable from your test machine for the hub URL.

Summary

Grid architecture uses a hub to control many nodes (machines with browsers).

Tests connect to the hub, which sends them to the right node.

This helps run tests faster and on many browsers at once.