Challenge - 5 Problems
CSS Selector Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
📝 Syntax
intermediate2:00remaining
Find element using CSS selector syntax
What is the output of this code snippet when trying to find an element with class
button-primary?Selenium Python
from selenium import webdriver from selenium.webdriver.common.by import By browser = webdriver.Chrome() browser.get('https://example.com') button = browser.find_element(By.CSS_SELECTOR, '.button-primary') print(button.tag_name) browser.quit()
Attempts:
2 left
💡 Hint
Remember that
find_element returns the first matching element and By.CSS_SELECTOR expects a valid CSS selector string.✗ Incorrect
The code uses Selenium's
find_element with By.CSS_SELECTOR to find the first element with class 'button-primary'. It then prints the tag name of that element. If the element exists, it prints the tag name like 'button'.🧠 Conceptual
intermediate2:00remaining
Understanding CSS selector specificity in Selenium
Which CSS selector will find all
<input> elements with attribute type='text' inside a form with id loginForm?Attempts:
2 left
💡 Hint
Think about how CSS selectors target elements by id, tag, and attribute.
✗ Incorrect
Option A correctly selects all
input elements with type='text' inside the element with id loginForm. The id selector #loginForm targets the form, then input[type='text'] selects inputs with that attribute inside it.❓ rendering
advanced2:00remaining
What happens when using an invalid CSS selector in Selenium?
Consider this code snippet trying to find an element with an invalid CSS selector. What error will Selenium raise?
Selenium Python
from selenium import webdriver from selenium.webdriver.common.by import By browser = webdriver.Chrome() browser.get('https://example.com') browser.find_element(By.CSS_SELECTOR, 'div#main > > p') browser.quit()
Attempts:
2 left
💡 Hint
Invalid CSS syntax causes a specific Selenium exception.
✗ Incorrect
The selector 'div#main > > p' is invalid CSS syntax due to consecutive '>' combinators. Selenium raises InvalidSelectorException when the CSS selector is malformed.
❓ selector
advanced2:00remaining
Selecting nested elements with CSS selectors in Selenium
Which CSS selector correctly finds all
<li> elements that are direct children of <ul> with class menu?Attempts:
2 left
💡 Hint
The '>' combinator selects direct children only.
✗ Incorrect
Option D uses 'ul.menu > li' which selects
li elements that are direct children of ul elements with class 'menu'. Option D selects all li descendants, not just direct children.❓ accessibility
expert3:00remaining
Using CSS selectors to find elements with ARIA attributes in Selenium
Which CSS selector will find all elements with the ARIA role
button that are also focusable (have tabindex='0')?Attempts:
2 left
💡 Hint
ARIA roles use the attribute 'role', not 'aria-role'. Also, attribute values should be quoted strings.
✗ Incorrect
Option C correctly selects elements with attribute role='button' and tabindex='0'. Option C uses a wrong attribute name 'aria-role'. Option C misses quotes around attribute values which is invalid in CSS selectors. Option C selects children with tabindex='0' inside elements with role='button', not the same element.