0
0
Selenium Pythontesting~15 mins

Grid setup and configuration in Selenium Python - Build an Automation Script

Choose your learning style9 modes available
Verify Selenium Grid setup by running a test on remote node
Preconditions (3)
Step 1: Create a remote WebDriver session connecting to the Grid Hub URL
Step 2: Request a Chrome browser session from the Grid
Step 3: Navigate to https://example.com
Step 4: Verify the page title is 'Example Domain'
Step 5: Close the browser session
✅ Expected Result: Test runs successfully on remote node via Selenium Grid and verifies the page title matches 'Example Domain'
Automation Requirements - selenium
Assertions Needed:
Assert the page title equals 'Example Domain'
Best Practices:
Use Remote WebDriver with desired capabilities
Use explicit waits if needed
Close the browser session after test
Handle exceptions gracefully
Automated Solution
Selenium Python
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC


def test_grid_remote_chrome():
    # Setup Chrome options
    chrome_options = Options()
    chrome_options.add_argument('--headless')  # Run headless for faster execution

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

    # Create Remote WebDriver session
    driver = webdriver.Remote(
        command_executor=grid_url,
        options=chrome_options
    )

    try:
        # Navigate to example.com
        driver.get('https://example.com')

        # Wait until title is present
        WebDriverWait(driver, 10).until(EC.title_is('Example Domain'))

        # Assert page title
        assert driver.title == 'Example Domain', f"Expected title 'Example Domain' but got '{driver.title}'"

    finally:
        # Close browser session
        driver.quit()


if __name__ == '__main__':
    test_grid_remote_chrome()

This script connects to a Selenium Grid Hub running locally on port 4444.

It uses webdriver.Remote with Chrome options to request a Chrome browser session on a remote node.

The test navigates to https://example.com and waits explicitly until the page title matches 'Example Domain'.

An assertion checks the page title to verify the page loaded correctly.

Finally, the browser session is closed with driver.quit() to free resources.

This setup confirms the Grid is correctly routing commands to a remote node.

Common Mistakes - 4 Pitfalls
Using local WebDriver instead of Remote WebDriver
Not specifying browser options or capabilities
Not closing the browser session after test
Hardcoding waits instead of using explicit waits
Bonus Challenge

Now add data-driven testing to run the same test on Chrome and Firefox browsers via the Grid

Show Hint