How to Use Selenium Grid for Distributed Testing in Python

How to Use Selenium Grid for Distributed Testing in Python

Using Selenium Grid for distributed testing in Python allows you to run your Selenium test scripts on multiple machines (nodes) and browsers in parallel. This is especially useful for scaling your test automation.


๐Ÿ•ธ️ What is Selenium Grid?

Selenium Grid lets you:


Run tests in parallel across multiple machines.


Run tests across different browsers and OS combinations.


Use a central Hub to manage test execution on distributed Nodes.


๐Ÿงฑ Components of Selenium Grid

Hub: Central server that receives test requests and routes them to appropriate nodes.


Node: Machine where browsers are launched and tests are executed.


๐Ÿงช Step-by-Step: Using Selenium Grid with Python

✅ Step 1: Install Selenium

bash

Copy

Edit

pip install selenium

✅ Step 2: Download Selenium Server

Download the Selenium Server JAR (Grid) from:

๐Ÿ‘‰ https://www.selenium.dev/downloads/


Place the .jar file somewhere accessible.


✅ Step 3: Start the Hub

Run this in a terminal:


bash

Copy

Edit

java -jar selenium-server-<version>.jar hub

By default, Hub runs on http://localhost:4444.


✅ Step 4: Start a Node and Register It to the Hub

Run this on the same or another machine:


bash

Copy

Edit

java -jar selenium-server-<version>.jar node --detect-drivers true

This will register the Node to the Hub.


๐Ÿ’ก Make sure the Node has necessary browser drivers (like chromedriver, geckodriver).


✅ Step 5: Python Script for Remote WebDriver

Now write your Python test to connect to the Grid:


python

Copy

Edit

from selenium import webdriver

from selenium.webdriver.common.desired_capabilities import DesiredCapabilities


# Set the Grid Hub URL

grid_url = "http://localhost:4444/wd/hub"


# Choose browser

driver = webdriver.Remote(

    command_executor=grid_url,

    desired_capabilities=DesiredCapabilities.CHROME

)


driver.get("https://www.google.com")

print(driver.title)

driver.quit()

You can replace DesiredCapabilities.CHROME with FIREFOX, EDGE, etc.


⚙️ Optional: Docker-Based Selenium Grid

You can run a full Selenium Grid (hub + browsers) with Docker:


bash

Copy

Edit

# Run hub + Chrome + Firefox nodes

docker run -d -p 4444:4444 --name selenium-grid selenium/standalone-chrome

Then connect your Python script to http://localhost:4444/wd/hub.


๐Ÿงช Benefits of Using Selenium Grid

Feature Benefit

Parallel test execution Faster test runs

Cross-browser testing Test on Chrome, Firefox, Edge, etc.

Cross-platform support Run on Windows, macOS, Linux, Docker

Scalability Add/remove nodes as needed


๐Ÿ” Troubleshooting Tips

Ensure your Hub and Nodes are on the same network (or properly routed).


Match driver versions with browser versions.


Check for firewall or port conflicts (default port is 4444).


Use http://localhost:4444/ui in Selenium 4+ to see Grid status.


Would you like an example using pytest or Docker Compose for managing multiple nodes?

Learn Selenium Python Training in Hyderabad

Read More

Top 5 Projects You Can Build Using Selenium and Python

How Selenium with Python Fits into the QA Automation Career Path

Visit Our IHUB Talent Training Institute in Hyderabad

Get Directions

Comments

Popular posts from this blog

Handling Frames and Iframes Using Playwright

Tosca for API Testing: A Step-by-Step Tutorial

Cybersecurity Internship Opportunities in Hyderabad for Freshers