0
0
Selenium Pythontesting~5 mins

Running tests on Grid in Selenium Python

Choose your learning style9 modes available
Introduction

Running tests on Grid lets you test your website on many browsers and computers at the same time. This saves time and checks your site works everywhere.

You want to test your website on different browsers like Chrome and Firefox at once.
You need to run tests on different operating systems like Windows and Linux without having all machines.
You want to speed up testing by running many tests in parallel.
You want to share test resources across a team without setting up many local browsers.
You want to test on remote machines or cloud services easily.
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
)

# Use driver as usual

Replace <hub-ip> with your Selenium Grid Hub address.

DesiredCapabilities tells Grid which browser you want.

Examples
This connects to Grid Hub at IP 192.168.1.100 and requests a Firefox browser.
Selenium Python
driver = webdriver.Remote(
    command_executor='http://192.168.1.100:4444/wd/hub',
    desired_capabilities=DesiredCapabilities.FIREFOX
)
This requests a Chrome browser on Windows platform from the Grid.
Selenium Python
caps = DesiredCapabilities.CHROME.copy()
caps['platform'] = 'WINDOWS'
driver = webdriver.Remote(
    command_executor='http://localhost:4444/wd/hub',
    desired_capabilities=caps
)
Sample Program

This script connects to a Selenium Grid Hub running locally, opens Chrome remotely, navigates to 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'
caps = DesiredCapabilities.CHROME.copy()

# Start remote browser session
driver = webdriver.Remote(command_executor=hub_url, desired_capabilities=caps)

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

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

# Close browser
driver.quit()
OutputSuccess
Important Notes

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

Use driver.quit() to close the remote browser and free resources.

Grid allows running many tests at once on different machines to save time.

Summary

Running tests on Grid lets you test on many browsers and machines remotely.

You connect to the Grid Hub using webdriver.Remote and specify browser details.

This helps run tests faster and on different platforms without local setup.