0
0
Selenium Pythontesting~5 mins

Grid setup and configuration in Selenium Python

Choose your learning style9 modes available
Introduction

Grid setup lets you run tests on many browsers and machines at the same time. This saves time and checks your app works everywhere.

You want to test your website on Chrome and Firefox at the same time.
You need to run tests on different operating systems like Windows and Linux.
You want to speed up testing by running many tests in parallel.
You have a team and want to share test machines easily.
You want to test on real devices or remote servers.
Syntax
Selenium Python
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

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

Replace 'hub_ip' with your Selenium Grid hub address.

DesiredCapabilities sets the browser type for the test.

Examples
This example connects to a Grid hub at IP 192.168.1.100 and runs tests on Firefox.
Selenium Python
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

driver = webdriver.Remote(
    command_executor='http://192.168.1.100:4444/wd/hub',
    desired_capabilities=DesiredCapabilities.FIREFOX
)
This example requests Chrome browser on Windows platform from the Grid.
Selenium Python
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

caps = DesiredCapabilities.CHROME.copy()
caps['platformName'] = 'WINDOWS'

driver = webdriver.Remote(
    command_executor='http://hub.example.com:4444/wd/hub',
    desired_capabilities=caps
)
Sample Program

This script connects to a local Selenium Grid hub, opens example.com in Chrome, prints the page title, and closes the browser.

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

# Setup Remote WebDriver to connect to Selenium Grid hub
hub_url = 'http://localhost:4444/wd/hub'
driver = webdriver.Remote(
    command_executor=hub_url,
    desired_capabilities=DesiredCapabilities.CHROME
)

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

# Check page title
title = driver.title
print(f'Page title is: {title}')

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

Make sure the Selenium Grid hub and nodes are running before starting tests.

Use correct hub URL and port (default is 4444).

Nodes must support the requested browser and platform.

Summary

Selenium Grid lets you run tests on many browsers and machines at once.

Use webdriver.Remote with hub URL and desired capabilities to connect.

Always check your Grid setup before running tests.