Bird
Raised Fist0
Selenium Pythontesting~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What does the Selenium method find_element(By.CLASS_NAME, "example") do?
easy
A. Finds elements by their ID "example".
B. Finds all elements with the class name "example" on the webpage.
C. Finds elements by their tag name "example".
D. Finds the first element with the class name "example" on the webpage.

Solution

  1. Step 1: Understand the method purpose

    The method find_element locates a single element on the page.
  2. Step 2: Identify the locator type

    Using By.CLASS_NAME means it looks for the first element with the given class name.
  3. Final Answer:

    Finds the first element with the class name "example" on the webpage. -> Option D
  4. Quick Check:

    find_element + CLASS_NAME = first matching element [OK]
Hint: find_element with CLASS_NAME finds only the first matching element [OK]
Common Mistakes:
  • Confusing find_element with find_elements (plural)
  • Using class name but expecting multiple elements
  • Mixing class name with ID or tag name locators
2. Which of the following is the correct syntax to find an element by class name "btn-primary" using Selenium in Python?
easy
A. driver.find_element(By.CLASS_NAME, "btn-primary")
B. driver.find_element_by_class_name("btn-primary")
C. driver.find_element(By.CLASS, "btn-primary")
D. driver.find_element(By.CLASSNAME, "btn-primary")

Solution

  1. Step 1: Recall the correct Selenium syntax

    In Selenium Python 4+, the recommended syntax is find_element(By.CLASS_NAME, "class").
  2. Step 2: Check each option for correctness

    driver.find_element(By.CLASS_NAME, "btn-primary") uses the correct locator By.CLASS_NAME and method find_element. Others use deprecated or incorrect locators.
  3. Final Answer:

    driver.find_element(By.CLASS_NAME, "btn-primary") -> Option A
  4. Quick Check:

    Use By.CLASS_NAME with find_element [OK]
Hint: Use By.CLASS_NAME with find_element, not old deprecated methods [OK]
Common Mistakes:
  • Using deprecated find_element_by_class_name method
  • Wrong locator names like CLASS or CLASSNAME
  • Missing quotes around class name string
3. Given the HTML snippet:
<div class="alert success">Success!</div>
<div class="alert error">Error!</div>

What text will this Selenium Python code print?
element = driver.find_element(By.CLASS_NAME, "alert")
print(element.text)
medium
A. "Success!"
B. "Error!"
C. "Success! Error!"
D. Throws NoSuchElementException

Solution

  1. Step 1: Understand find_element behavior

    It finds the first element matching the class name "alert" in the HTML order.
  2. Step 2: Identify the first matching element

    The first <div> with class "alert success" contains text "Success!".
  3. Final Answer:

    "Success!" -> Option A
  4. Quick Check:

    find_element returns first matching element's text [OK]
Hint: find_element returns first match only, so text is from first element [OK]
Common Mistakes:
  • Assuming it returns all matching elements' text
  • Confusing class names with multiple classes
  • Expecting an error if multiple classes exist
4. What is wrong with this Selenium Python code snippet?
element = driver.find_element(By.CLASS_NAME, ".button")
medium
A. find_element should be find_elements to find class
B. Class name should not include the dot prefix "."
C. By.CLASS_NAME is not a valid locator
D. The class name must be uppercase

Solution

  1. Step 1: Check the class name format

    When using By.CLASS_NAME, the class name should be given without any CSS selector symbols like ".".
  2. Step 2: Identify the error in the code

    The code incorrectly includes a dot prefix ".button" which causes a failure to find the element.
  3. Final Answer:

    Class name should not include the dot prefix "." -> Option B
  4. Quick Check:

    Class name locator needs plain class name, no dot [OK]
Hint: Remove dot prefix when using By.CLASS_NAME locator [OK]
Common Mistakes:
  • Adding dot prefix like CSS selectors to class name
  • Using find_elements when only one element is needed
  • Assuming class names must be uppercase
5. You want to click the first button with class "submit-btn" on a page. Which code snippet correctly finds and clicks it using Selenium Python?
hard
A. button = driver.find_elements(By.CLASS_NAME, "submit-btn") button.click()
B. button = driver.find_element(By.ID, "submit-btn") button.click()
C. button = driver.find_element(By.CLASS_NAME, "submit-btn") button.click()
D. button = driver.find_element(By.CLASS_NAME, ".submit-btn") button.click()

Solution

  1. Step 1: Choose correct method to find single element by class

    Use find_element(By.CLASS_NAME, "submit-btn") to get the first matching element.
  2. Step 2: Call click() on the found element

    After locating the element, call click() to perform the click action.
  3. Final Answer:

    button = driver.find_element(By.CLASS_NAME, "submit-btn")\nbutton.click() -> Option C
  4. Quick Check:

    find_element + CLASS_NAME + click() = correct usage [OK]
Hint: Use find_element with class name, then call click() on it [OK]
Common Mistakes:
  • Using find_elements returns list, cannot call click() directly
  • Using ID locator instead of class name
  • Including dot prefix in class name
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard