0
0
Selenium Pythontesting~15 mins

Grid architecture (hub and node) in Selenium Python - Build an Automation Script

Choose your learning style9 modes available
Verify Google Search on Selenium Grid Hub and Node
Preconditions (3)
Step 1: Open a remote Chrome browser session via Selenium Grid Hub
Step 2: Navigate to https://www.google.com
Step 3: Enter 'Selenium Grid' in the search input field
Step 4: Submit the search form
Step 5: Wait for the search results page to load
Step 6: Verify the page title contains 'Selenium Grid'
✅ Expected Result: The remote browser controlled via Selenium Grid performs a Google search and the page title contains 'Selenium Grid', confirming the test ran on the Grid setup.
Automation Requirements - Selenium WebDriver with Python
Assertions Needed:
Page title contains the search term 'Selenium Grid'
Best Practices:
Use Remote WebDriver to connect to Selenium Grid Hub
Use explicit waits to wait for page elements or title
Use By locators with clear identifiers
Close the browser session after test
Automated Solution
Selenium Python
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

# URL of the Selenium Grid Hub
hub_url = 'http://localhost:4444/wd/hub'

# Desired capabilities for Chrome browser
capabilities = {
    'browserName': 'chrome'
}

# Create Remote WebDriver session
with webdriver.Remote(command_executor=hub_url, desired_capabilities=capabilities) as driver:
    wait = WebDriverWait(driver, 10)
    # Step 1: Navigate to Google
    driver.get('https://www.google.com')

    # Step 2: Find the search input field
    search_box = wait.until(EC.presence_of_element_located((By.NAME, 'q')))

    # Step 3: Enter search term and submit
    search_term = 'Selenium Grid'
    search_box.send_keys(search_term + Keys.RETURN)

    # Step 4: Wait for title to contain search term
    wait.until(EC.title_contains(search_term))

    # Step 5: Assert title contains search term
    assert search_term in driver.title, f"Expected '{search_term}' in title but got '{driver.title}'"

    # Browser will close automatically due to 'with' context

This script connects to the Selenium Grid Hub running locally on port 4444 using webdriver.Remote. It requests a Chrome browser session on a registered Node.

We use explicit waits (WebDriverWait) to wait for the Google search input to appear and then for the page title to update after submitting the search.

The assertion checks that the page title contains the search term 'Selenium Grid', confirming the search worked and the test ran on the Grid.

Using the with statement ensures the browser session closes cleanly after the test.

Common Mistakes - 4 Pitfalls
Using local WebDriver instead of Remote WebDriver to connect to Grid
Hardcoding implicit sleeps instead of using explicit waits
Using brittle locators like absolute XPath for search input
Not closing the browser session after test
Bonus Challenge

Now add data-driven testing with 3 different search terms: 'Selenium Grid', 'Python testing', 'WebDriver automation'

Show Hint