0
0
Selenium Pythontesting~15 mins

Selenium components (WebDriver, Grid, IDE) in Selenium Python - Build an Automation Script

Choose your learning style9 modes available
Verify Google Search using Selenium WebDriver
Preconditions (3)
Step 1: Open Chrome browser using Selenium WebDriver
Step 2: Navigate to 'https://www.google.com'
Step 3: Locate the search input field by its name attribute 'q'
Step 4: Enter the search term 'Selenium WebDriver'
Step 5: Submit the search form
Step 6: Wait for the search results page to load
Step 7: Verify that the page title contains the search term 'Selenium WebDriver'
✅ Expected Result: The browser opens Google homepage, performs the search, and the results page title contains 'Selenium WebDriver'
Automation Requirements - Selenium WebDriver with Python
Assertions Needed:
Verify page title contains the search term after search
Verify search input field is present before typing
Best Practices:
Use explicit waits to wait for elements or page conditions
Use By locators for element identification
Close the browser after test execution
Handle exceptions gracefully
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


def test_google_search():
    # Initialize Chrome WebDriver
    driver = webdriver.Chrome()
    try:
        # Open Google homepage
        driver.get('https://www.google.com')

        # Wait until the search box is present
        wait = WebDriverWait(driver, 10)
        search_box = wait.until(EC.presence_of_element_located((By.NAME, 'q')))

        # Enter search term and submit
        search_term = 'Selenium WebDriver'
        search_box.send_keys(search_term + Keys.RETURN)

        # Wait until title contains search term
        wait.until(EC.title_contains(search_term))

        # Assert title contains search term
        assert search_term in driver.title, f"Title does not contain '{search_term}'"

    finally:
        # Close the browser
        driver.quit()


if __name__ == '__main__':
    test_google_search()

This script uses Selenium WebDriver with Python to automate the manual test case.

First, it opens the Chrome browser and navigates to Google.

It waits explicitly for the search input field to appear using WebDriverWait and expected_conditions.

Then it types the search term and submits the form by sending the RETURN key.

Next, it waits until the page title contains the search term to ensure the results page loaded.

Finally, it asserts the title contains the search term and closes the browser in a finally block to ensure cleanup.

This approach follows best practices: using explicit waits, proper locators, and clean resource management.

Common Mistakes - 4 Pitfalls
Using time.sleep() instead of explicit waits
Using incorrect locator strategies like XPath with absolute paths
Not closing the browser after test
Not verifying element presence before interacting
Bonus Challenge

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

Show Hint