0
0
Selenium Pythontesting~20 mins

Clearing input fields in Selenium Python - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Input Field Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this Selenium Python code snippet?
Consider the following Selenium Python code that tries to clear an input field and then enter new text. What will be the value of the input field after execution?
Selenium Python
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

input_element = WebDriverWait(driver, 10).until(
    EC.presence_of_element_located((By.ID, "username"))
)
input_element.send_keys("old_value")
input_element.clear()
input_element.send_keys("new_value")
value_after = input_element.get_attribute("value")
print(value_after)
A"new_value"
B"old_value"
C""
DNone
Attempts:
2 left
💡 Hint
Remember that clear() removes existing text before sending new keys.
assertion
intermediate
1:30remaining
Which assertion correctly verifies that an input field is empty after clearing?
You have cleared an input field using Selenium's clear() method. Which assertion correctly checks that the input field is empty?
Selenium Python
input_element.clear()
value = input_element.get_attribute("value")
Aassert value == ' '
Bassert value == None
Cassert value is None
Dassert value == ""
Attempts:
2 left
💡 Hint
The value attribute of an empty input field is an empty string, not None or space.
locator
advanced
1:30remaining
Which locator is best to find an input field for clearing text?
You want to clear the text from an input field uniquely identified on the page. Which locator strategy is best practice for this purpose?
Adriver.find_element(By.XPATH, "//input[@type='text']")
Bdriver.find_element(By.CLASS_NAME, "input-field")
Cdriver.find_element(By.ID, "user-email")
Ddriver.find_element(By.TAG_NAME, "input")
Attempts:
2 left
💡 Hint
Choose the locator that is most specific and least likely to change.
🔧 Debug
advanced
2:00remaining
Why does clear() not remove text in this Selenium test?
This Selenium Python code tries to clear an input field but the text remains after execution. What is the most likely reason?
Selenium Python
input_element = driver.find_element(By.ID, "search-box")
input_element.clear()
input_element.send_keys("query")
AThe input field is disabled or readonly, so clear() has no effect.
BThe clear() method is deprecated and does not work anymore.
CThe locator By.ID is incorrect and finds no element.
Dsend_keys("query") overwrites clear(), so text remains old.
Attempts:
2 left
💡 Hint
Check if the input field allows editing before clearing.
framework
expert
3:00remaining
How to implement a reusable method to clear and enter text safely in Selenium Python?
You want to create a reusable function that clears an input field and enters new text, waiting until the field is ready. Which implementation is correct?
A
def clear_and_type(driver, locator, text):
    element = driver.find_element(*locator)
    element.clear()
    element.send_keys(text)
B
def clear_and_type(driver, locator, text):
    element = WebDriverWait(driver, 10).until(
        EC.element_to_be_clickable(locator)
    )
    element.clear()
    element.send_keys(text)
C
def clear_and_type(driver, locator, text):
    element = WebDriverWait(driver, 10).until(
        EC.presence_of_element_located(locator)
    )
    element.clear()
    element.send_keys(text)
D
def clear_and_type(driver, locator, text):
    element = WebDriverWait(driver, 10).until(
        EC.visibility_of_element_located(locator)
    )
    element.send_keys(text)
    element.clear()
Attempts:
2 left
💡 Hint
Wait until the element is clickable before clearing and typing.