0
0
Selenium Pythontesting~20 mins

Find element by class name in Selenium Python - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Class Name Locator Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
locator
intermediate
2:00remaining
Identify the correct Selenium locator for class name
Which of the following Selenium Python code snippets correctly finds an element by its class name btn-primary?
Adriver.find_element(By.CLASS_NAME, "btn-primary")
Bdriver.find_element(By.CLASS, "btn-primary")
Cdriver.find_element_by_class_name("btn-primary")
Ddriver.find_element(By.CSS_SELECTOR, ".btn-primary")
Attempts:
2 left
💡 Hint
Use the modern Selenium syntax with By.CLASS_NAME for class selectors.
Predict Output
intermediate
2:00remaining
Output of Selenium find_element by class name with multiple matches
Given the HTML below, what will driver.find_element(By.CLASS_NAME, "item") return?
Selenium Python
<div class="item">First</div>
<div class="item">Second</div>
<div class="item">Third</div>
AThe last <div> element with class 'item' containing text 'Third'
BA list of all three <div> elements with class 'item'
CAn error because multiple elements have the same class
DThe first <div> element with class 'item' containing text 'First'
Attempts:
2 left
💡 Hint
find_element returns only one element, not a list.
assertion
advanced
2:00remaining
Correct assertion to verify element text found by class name
You found an element by class name message. Which assertion correctly verifies its text is exactly Success?
Selenium Python
element = driver.find_element(By.CLASS_NAME, "message")
text = element.text
Aassert text.equals("Success")
Bassert text == "Success"
Cassert text.contains("Success")
Dassert text is "Success"
Attempts:
2 left
💡 Hint
Use Python's equality operator to compare strings.
🔧 Debug
advanced
2:00remaining
Identify the error in Selenium code using class name locator
What error will this code raise?
element = driver.find_element(By.CLASS_NAME, "btn primary")
Selenium Python
from selenium.webdriver.common.by import By

# Attempt to find element by class name with space
element = driver.find_element(By.CLASS_NAME, "btn primary")
ASyntaxError
BNoSuchElementException
Cselenium.common.exceptions.InvalidSelectorException
DTypeError
Attempts:
2 left
💡 Hint
Class name locators cannot contain spaces because they represent a single class.
framework
expert
2:00remaining
Best practice for waiting for element by class name in Selenium
Which code snippet correctly waits up to 10 seconds for an element with class name loading-complete to appear before proceeding?
AWebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CLASS_NAME, "loading-complete")))
B
time.sleep(10)
driver.find_element(By.CLASS_NAME, "loading-complete")
Cdriver.find_element(By.CLASS_NAME, "loading-complete") # No wait
D
driver.implicitly_wait(10)
driver.find_element(By.CLASS_NAME, "loading-complete")
Attempts:
2 left
💡 Hint
Explicit waits are preferred for waiting specific conditions.