0
0
Selenium Pythontesting~15 mins

CSS attribute selectors in Selenium Python - Build an Automation Script

Choose your learning style9 modes available
Verify search input field using CSS attribute selector
Preconditions (2)
Step 1: Locate the search input field using a CSS attribute selector for input elements with type='search'
Step 2: Enter the text 'Selenium testing' into the search input field
Step 3: Submit the search form by pressing Enter key
Step 4: Wait for the search results page to load
✅ Expected Result: The search results page is displayed and the URL contains the search query 'Selenium testing'
Automation Requirements - Selenium with Python
Assertions Needed:
Verify the search input field is found using CSS attribute selector
Verify the search input field accepts the text input
Verify the page URL contains the search query after submission
Best Practices:
Use explicit waits to wait for elements and page load
Use By.CSS_SELECTOR with attribute selector syntax
Avoid hardcoded sleeps
Use clear and descriptive variable names
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

# Initialize the Chrome driver
with webdriver.Chrome() as driver:
    driver.get('https://example.com')  # Replace with actual homepage URL

    wait = WebDriverWait(driver, 10)

    # Locate search input using CSS attribute selector for input[type='search']
    search_input = wait.until(
        EC.presence_of_element_located((By.CSS_SELECTOR, "input[type='search']"))
    )

    # Verify the search input is displayed
    assert search_input.is_displayed(), "Search input field is not visible"

    # Enter text into search input
    search_query = 'Selenium testing'
    search_input.clear()
    search_input.send_keys(search_query)

    # Submit the search by pressing Enter
    search_input.send_keys(Keys.ENTER)

    # Wait for URL to contain the search query
    wait.until(EC.url_contains(search_query.replace(' ', '+')))

    # Verify URL contains the search query
    current_url = driver.current_url
    assert search_query.replace(' ', '+') in current_url, f"URL does not contain search query. URL: {current_url}"

The script starts by opening the browser and navigating to the homepage.

It uses an explicit wait to find the search input field using a CSS attribute selector input[type='search']. This ensures the element is present before interacting.

We assert the input is visible to confirm it can be used.

Then, the script clears any existing text and types the search query 'Selenium testing'.

Pressing Enter submits the search form.

We wait until the URL contains the search query (spaces replaced by '+') to confirm the search results page loaded.

Finally, an assertion checks the URL contains the query string, verifying the search worked.

This approach uses best practices like explicit waits, clear selectors, and meaningful assertions.

Common Mistakes - 4 Pitfalls
{'mistake': 'Using hardcoded sleep instead of explicit waits', 'why_bad': 'Hardcoded sleeps slow tests and can cause flaky failures if the page loads slower or faster than expected.', 'correct_approach': "Use Selenium's explicit waits like WebDriverWait with expected_conditions to wait for elements or URL changes."}
{'mistake': 'Using incorrect CSS selector syntax like missing quotes in attribute selector', 'why_bad': 'Incorrect CSS selectors cause element not found errors and test failures.', 'correct_approach': "Use correct CSS attribute selector syntax, e.g., input[type='search'] with quotes around attribute value."}
Not verifying element visibility before interacting
{'mistake': 'Hardcoding URLs or search queries without handling spaces or encoding', 'why_bad': 'URLs may not match expected format causing assertion failures.', 'correct_approach': "Replace spaces with '+' or use URL encoding when checking URL contents."}
Bonus Challenge

Now add data-driven testing with 3 different search queries

Show Hint