CSS attribute selectors in Selenium Python - Build an Automation Script
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.
Now add data-driven testing with 3 different search queries
Practice
input[type='text'] select in Selenium?Solution
Step 1: Understand the attribute selector syntax
The selectorinput[type='text']targetsinputtags with an attributetypeexactly equal to 'text'.Step 2: Match the selector meaning to options
All input elements with type attribute exactly equal to 'text' correctly describes this exact match. Other options describe different or incorrect matches.Final Answer:
All input elements with type attribute exactly equal to 'text' -> Option DQuick Check:
Exact attribute match = C [OK]
- Confusing exact match with partial match
- Selecting wrong tag type
- Mixing class selectors with attribute selectors
data-id that starts with 'user'?Solution
Step 1: Recall CSS attribute selectors for matching start of value
The caret symbol^=means 'starts with' in CSS attribute selectors.Step 2: Match the selector to the question
[data-id^='user'] uses[data-id^='user'], which selects elements whosedata-idattribute starts with 'user'.Final Answer:
[data-id^='user'] -> Option AQuick Check:
Starts with selector = B [OK]
- Using *= instead of ^= for starts with
- Using $= which means ends with
- Using exact match = instead of partial
<input> elements with name attributes: 'user1', 'user2', and 'admin'?
elements = driver.find_elements(By.CSS_SELECTOR, "input[name*='user']") print(len(elements))
Solution
Step 1: Understand the CSS selector
This selector findsinput[name*='user']inputelements whosenameattribute contains the substring 'user'.Step 2: Count matching elements
Given names 'user1', 'user2', and 'admin', only 'user1' and 'user2' contain 'user'. So, 2 elements match.Final Answer:
2 -> Option AQuick Check:
Substring match count = 2 [OK]
- Counting all inputs instead of filtered ones
- Confusing starts with (^) and contains (*) selectors
- Assuming exact match instead of partial
element = driver.find_element(By.CSS_SELECTOR, "button[class^='submit']'")What is the error and how to fix it?
Solution
Step 1: Inspect the CSS selector string syntax
The selector ends with an extra single quote after the closing double quote:"button[class^='submit']'". This causes a syntax error.Step 2: Correct the string by removing the extra quote
Removing the extra single quote at the end fixes the syntax:"button[class^='submit']".Final Answer:
Extra single quote at the end; remove it to fix -> Option BQuick Check:
Check quotes balance = A [OK]
- Ignoring syntax errors caused by quotes
- Changing correct attribute selectors unnecessarily
- Assuming CSS does not support ^= selector
<a> tags whose href attribute ends with '.pdf' or '.docx' using a single CSS selector in Selenium. Which selector is correct?Solution
Step 1: Understand CSS selector for multiple attribute matches
To select elements matching either condition, use a comma to separate selectors:a[href$='.pdf'], a[href$='.docx'].Step 2: Analyze other options for correctness
a[href$='.pdf' || href$='.docx'] uses invalid syntax with || inside attribute selector. a[href$='.pdf'][href$='.docx'] requires both endings simultaneously, impossible. a[href*='.pdf' or '.docx'] uses invalid syntax with 'or'.Final Answer:
a[href$='.pdf'], a[href$='.docx'] -> Option CQuick Check:
Use comma to combine selectors = D [OK]
- Using logical operators inside attribute selectors
- Trying to combine multiple attribute endings in one selector without comma
- Assuming multiple attribute selectors mean OR instead of AND
Ready to prove it?
Pass the test and this pattern moves unknown
