CSS selector syntax 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() try: # Open the login page driver.get('https://example.com/login') # Wait until the login button is clickable using CSS selector wait = WebDriverWait(driver, 10) login_button = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, '#loginBtn'))) # Click the login button login_button.click() # Wait until URL changes to dashboard wait.until(EC.url_to_be('https://example.com/dashboard')) # Assert the current URL is dashboard assert driver.current_url == 'https://example.com/dashboard', f"Expected URL to be 'https://example.com/dashboard' but got {driver.current_url}" finally: driver.quit()
This script uses Selenium WebDriver with Python to automate the manual test case.
First, it opens the login page URL.
Then, it waits explicitly for the login button to be clickable using the CSS selector #loginBtn. This ensures the element is ready for interaction.
Next, it clicks the login button.
After clicking, it waits until the URL changes to the expected dashboard URL.
Finally, it asserts that the current URL matches the expected dashboard URL to confirm navigation succeeded.
The try-finally block ensures the browser closes even if the test fails.
This approach follows best practices: using explicit waits, CSS selectors, and clear assertions.
Now add data-driven testing with 3 different CSS selectors for the login button
Practice
button?Solution
Step 1: Understand class selector syntax
In CSS, a class selector uses a dot.followed by the class name to select elements with that class.Step 2: Match the selector to the class
The selectorbutton.buttonselects all elements with classbutton.Final Answer:
.button -> Option AQuick Check:
Class selector = .classname [OK]
- Using # for class instead of .
- Using tag name without dot
- Using * which is universal selector
main-header?Solution
Step 1: Understand ID selector syntax
ID selectors use a hash#followed by the ID name to select a unique element.Step 2: Match selector to ID
The selectormain-header#main-headerselects the element with IDmain-header.Final Answer:
#main-header -> Option DQuick Check:
ID selector = #idname [OK]
- Using dot (.) for ID instead of hash (#)
- Using tag name without prefix
- Adding * before ID selector unnecessarily
<div class='container'> <button id='submit' class='btn primary'>Send</button> </div>
Which CSS selector will select the
button element inside the div with class container?Solution
Step 1: Understand descendant selector
div.container buttonselects anybuttoninside adivwith classcontainer.Step 2: Check other options for correctness
container > button.primaryincorrectly targets a <container> tag (missing dot for class selector).#submit.containeris invalid because ID and class cannot be combined like that without space.button#submit .containerlooks for.containerinsidebutton#submit, which is wrong.Final Answer:
div.container button -> Option BQuick Check:
Descendant selector = parent child [OK]
- Confusing descendant and child selectors
- Combining ID and class without space
- Wrong order of selectors
input elements with attribute type='text'. Which CSS selector is correct?Solution
Step 1: Understand attribute selector syntax
CSS attribute selectors can use quotes or no quotes around the value. Bothinput[type=text]andinput[type='text']are valid.Step 2: Confirm all options are valid
Using double quotesinput[type="text"]is also valid. So all three selectors correctly selectinputelements withtype='text'.Final Answer:
All of the above -> Option AQuick Check:
Attribute selectors allow quotes or no quotes [OK]
- Using incorrect attribute syntax
- Forgetting quotes when value has spaces
- Using wrong attribute name
li elements that are the first child of their parent ul. Which CSS selector should you use?Solution
Step 1: Understand child combinator and pseudo-class
ul > li:first-childselectslielements that are the first child directly underul.Step 2: Compare other options
ul li:first-childselects anylithat is first child of its parent inside anyuldescendant (could be nested).li:first-childselects anylithat is first child anywhere.ul li:first-of-typeselects firstliof its type insideul, but not necessarily first child.Final Answer:
ul > li:first-child -> Option CQuick Check:
Direct child + first-child pseudo-class = ul > li:first-child [OK]
- Confusing :first-child and :first-of-type
- Missing direct child combinator >
- Selecting nested elements unintentionally
Ready to prove it?
Pass the test and this pattern moves unknown
