Verify Google Search on Selenium Grid Hub and Node
Preconditions (3)
✅ 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.
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.
Now add data-driven testing with 3 different search terms: 'Selenium Grid', 'Python testing', 'WebDriver automation'
hub in Selenium Grid architecture?from selenium import webdriver from selenium.webdriver.common.desired_capabilities import DesiredCapabilities hub_url = "http://localhost:4444/wd/hub" driver = webdriver.Remote(command_executor=hub_url, desired_capabilities=DesiredCapabilities.CHROME) print(driver.capabilities['browserName']) driver.quit()
java -jar selenium-server.jar -role node -hub http://localhost:4444/grid/registerPass the test and this pattern moves unknown