0
0
Selenium Pythontesting~20 mins

Find element by CSS selector in Selenium Python - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
CSS Selector Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
📝 Syntax
intermediate
2: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()
APrints the tag name of the first element with class 'button-primary', e.g., 'button'
BRaises a SyntaxError due to incorrect CSS selector syntax
CRaises a NoSuchElementException if no element with class 'button-primary' is found
DPrints the text content of the element instead of the tag name
Attempts:
2 left
💡 Hint
Remember that find_element returns the first matching element and By.CSS_SELECTOR expects a valid CSS selector string.
🧠 Conceptual
intermediate
2: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?
A#loginForm input[type='text']
Binput#loginForm[type='text']
Cinput[type='text'] > #loginForm
Dform input[type=text]#loginForm
Attempts:
2 left
💡 Hint
Think about how CSS selectors target elements by id, tag, and attribute.
rendering
advanced
2: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()
Aselenium.common.exceptions.ElementNotInteractableException
Bselenium.common.exceptions.InvalidSelectorException
CNo error, returns None
Dselenium.common.exceptions.NoSuchElementException
Attempts:
2 left
💡 Hint
Invalid CSS syntax causes a specific Selenium exception.
selector
advanced
2: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?
Aul > .menu li
Bul.menu li
Cli > ul.menu
Dul.menu > li
Attempts:
2 left
💡 Hint
The '>' combinator selects direct children only.
accessibility
expert
3: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')?
A[role=button][tabindex=0]
B[aria-role='button'][tabindex='0']
C[role='button'][tabindex='0']
D[role='button'] > [tabindex='0']
Attempts:
2 left
💡 Hint
ARIA roles use the attribute 'role', not 'aria-role'. Also, attribute values should be quoted strings.