CSS pseudo-classes for selection in Selenium Python - Build an Automation Script
from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC # Setup WebDriver (assuming chromedriver is in PATH) driver = webdriver.Chrome() wait = WebDriverWait(driver, 10) try: # Open the test page driver.get('https://example.com/test-list') # Replace with actual test page URL # Wait for the list container to be present wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, 'ul#item-list'))) # Locate elements using CSS pseudo-classes first_item = driver.find_element(By.CSS_SELECTOR, 'ul#item-list li:first-child') last_item = driver.find_element(By.CSS_SELECTOR, 'ul#item-list li:last-child') third_item = driver.find_element(By.CSS_SELECTOR, 'ul#item-list li:nth-child(3)') even_items = driver.find_elements(By.CSS_SELECTOR, 'ul#item-list li:nth-child(even)') odd_items = driver.find_elements(By.CSS_SELECTOR, 'ul#item-list li:nth-child(odd)') # Assertions assert first_item.text == 'Item 1', f"Expected 'Item 1' but got '{first_item.text}'" assert last_item.text == 'Item 5', f"Expected 'Item 5' but got '{last_item.text}'" assert third_item.text == 'Item 3', f"Expected 'Item 3' but got '{third_item.text}'" even_texts = [item.text for item in even_items] assert even_texts == ['Item 2', 'Item 4'], f"Expected ['Item 2', 'Item 4'] but got {even_texts}" odd_texts = [item.text for item in odd_items] assert odd_texts == ['Item 1', 'Item 3', 'Item 5'], f"Expected ['Item 1', 'Item 3', 'Item 5'] but got {odd_texts}" finally: driver.quit()
This script uses Selenium WebDriver with Python to automate the test case.
First, it opens the test page URL where the list is located.
It waits explicitly for the list container to be present to avoid timing issues.
Then, it uses CSS selectors with pseudo-classes like :first-child, :last-child, :nth-child(3), :nth-child(even), and :nth-child(odd) to locate specific list items.
Assertions check that the text content of these elements matches the expected values, with clear messages to help identify failures.
Finally, the browser is closed cleanly in a finally block to ensure resources are freed.
Now add data-driven testing with 3 different lists having different item counts and verify pseudo-class selections accordingly
Practice
Solution
Step 1: Understand the meaning of :first-child
The :first-child pseudo-class selects the very first child element of its parent.Step 2: Compare with other options
:last-child selects the last child, :nth-child(2) selects the second child, and :hover is for mouse hover state, not position.Final Answer:
:first-child -> Option BQuick Check:
First child selector = :first-child [OK]
- Confusing :first-child with :nth-child(1)
- Using :hover for position selection
- Mixing :last-child with :first-child
Solution
Step 1: Understand nth-child(even)
The selector li:nth-child(even) selects all list items at even positions: 2, 4, 6, etc.Step 2: Check other options
li:nth-child(2n+1) and li:nth-child(odd) select odd positions, li:nth-of-type(odd) selects odd elements but by type, which may differ.Final Answer:
li:nth-child(even) -> Option DQuick Check:
Even items = nth-child(even) [OK]
- Using odd instead of even
- Confusing nth-child with nth-of-type
- Wrong formula like 2n+1 for even
<ul> <li>Apple</li> <li>Banana</li> <li>Cherry</li> <li>Date</li> </ul>
What text will be selected by the CSS selector
ul li:nth-child(3) in Selenium?Solution
Step 1: Understand nth-child(3) selector
The selector ul li:nth-child(3) selects the third child li element inside the ul.Step 2: Identify the third li element
The third li element is <li>Cherry</li> as it is the third item in the list.Final Answer:
Cherry -> Option AQuick Check:
Third child text = Cherry [OK]
- Counting from zero instead of one
- Selecting nth-of-type instead of nth-child
- Confusing order of list items
driver.find_element(By.CSS_SELECTOR, 'div:nth-child(0)'). Why does it fail and how to fix it?Solution
Step 1: Check nth-child index rules
nth-child indexes start at 1, so 0 is invalid and causes a syntax error.Step 2: Correct the selector
Replace 'div:nth-child(0)' with 'div:nth-child(1)' to select the first child div correctly.Final Answer:
nth-child index starts at 1, so 0 is invalid; use 'div:nth-child(1)' instead -> Option AQuick Check:
nth-child index ≥ 1 [OK]
- Using zero as nth-child index
- Assuming nth-child works like array index
- Thinking Selenium lacks nth-child support
Solution
Step 1: Understand the difference between nth-child and nth-of-type
nth-child counts all types of children, nth-of-type counts only elements of the specified type (button).Step 2: Select buttons except the first one
Using 'form button:nth-of-type(n+2)' selects all button elements starting from the second button, ignoring other element types.Step 3: Why other options fail
:not(:first-child) excludes elements that are the first child of their parent, but if the first child is not a button, it won't exclude the first button. Similarly, :nth-child(n+2) counts all children, not just buttons, so it may skip buttons if other elements are present.Final Answer:
form button:nth-of-type(n+2) -> Option CQuick Check:
Use nth-of-type for type-specific position [OK]
- Using nth-child instead of nth-of-type
- Using :not(:first-child) which may exclude wrong elements
- Assuming first-child always matches first button
Ready to prove it?
Pass the test and this pattern moves unknown
