CSS attribute selectors help find web elements by matching their attributes. This makes tests more flexible and reliable.
CSS attribute selectors in Selenium Python
driver.find_element(By.CSS_SELECTOR, '[attribute="value"]')Use square brackets [] to specify the attribute and its value.
You can match exact values or use special operators for partial matches.
driver.find_element(By.CSS_SELECTOR, '[type="submit"]')driver.find_element(By.CSS_SELECTOR, '[name^="user"]')driver.find_element(By.CSS_SELECTOR, '[class*="btn-primary"]')driver.find_element(By.CSS_SELECTOR, '[data-test$="-button"]')This script opens a webpage with a custom checkbox. It uses CSS attribute selectors to find the label and checkbox input by their attributes. It prints the label text and checkbox state before and after clicking.
from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.chrome.service import Service from selenium.webdriver.chrome.options import Options import time options = Options() options.add_argument('--headless') service = Service() driver = webdriver.Chrome(service=service, options=options) try: driver.get('https://www.w3schools.com/howto/howto_css_custom_checkbox.asp') # Find checkbox label using attribute selector for 'for' attribute checkbox_label = driver.find_element(By.CSS_SELECTOR, 'label[for="myCheck"]') print('Label text:', checkbox_label.text) # Find checkbox input using attribute selector for type checkbox_input = driver.find_element(By.CSS_SELECTOR, 'input[type="checkbox"]') print('Checkbox is selected:', checkbox_input.is_selected()) # Click checkbox to change state checkbox_input.click() print('Checkbox is selected after click:', checkbox_input.is_selected()) finally: driver.quit()
Attribute selectors are case-sensitive in HTML.
Use quotes around attribute values in selectors for best practice.
Combine attribute selectors with other selectors for precise targeting.
CSS attribute selectors help find elements by their attributes.
They make tests more stable and easier to maintain.
Use them to match exact or partial attribute values.
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
