Challenge - 5 Problems
Clearing Fields Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this Selenium Java code snippet?
Consider the following Selenium Java code that attempts to clear a text input field and then check its content.
What will be the output printed to the console?
What will be the output printed to the console?
Selenium Java
WebElement inputField = driver.findElement(By.id("username")); inputField.sendKeys("hello"); inputField.clear(); String text = inputField.getAttribute("value"); System.out.println(text.isEmpty() ? "Field is empty" : "Field has text");
Attempts:
2 left
💡 Hint
Remember what the clear() method does to the input field's value.
✗ Incorrect
The clear() method empties the text input field. After clearing, the value attribute is an empty string, so text.isEmpty() returns true, printing 'Field is empty'.
❓ assertion
intermediate2:00remaining
Which assertion correctly verifies that a text field is cleared?
You want to write a JUnit assertion to confirm that a text input field is empty after calling clear(). Which assertion is correct?
Selenium Java
WebElement inputField = driver.findElement(By.name("email"));
inputField.clear();Attempts:
2 left
💡 Hint
Check which method returns the current text inside an input field.
✗ Incorrect
The getAttribute("value") method returns the current text in an input field. After clear(), it should be an empty string. getText() returns visible text, which for input fields is usually empty.
❓ locator
advanced2:00remaining
Which locator is best to find a text input field for clearing its content?
You want to clear the password field on a login page. Which locator is the most reliable and recommended to find this field?
Attempts:
2 left
💡 Hint
IDs are unique and fastest for locating elements.
✗ Incorrect
Using By.id is the most reliable and fastest locator if the element has a unique id attribute. XPath and CSS selectors are more fragile or slower. Class names may not be unique.
🔧 Debug
advanced2:00remaining
Why does this Selenium Java code fail to clear the input field?
The following code tries to clear a text field but the field still contains text after execution. What is the likely cause?
Selenium Java
WebElement inputField = driver.findElement(By.id("search")); inputField.sendKeys("query"); inputField.clear(); inputField.sendKeys(Keys.BACK_SPACE);
Attempts:
2 left
💡 Hint
Check what happens when you send a BACK_SPACE key after clearing.
✗ Incorrect
After clearing the field, sending BACK_SPACE adds a character (or triggers input), so the field is not empty. The clear() method itself works correctly.
❓ framework
expert3:00remaining
In a Selenium test framework, which approach ensures input fields are cleared before entering text?
You want to design a reusable method in your Selenium Java framework that safely clears any input field before typing new text. Which method implementation is best?
Selenium Java
public void enterText(WebElement field, String text) {
// Implementation here
}Attempts:
2 left
💡 Hint
Sometimes clear() alone does not work reliably on all browsers.
✗ Incorrect
Using CTRL+A and DELETE keys simulates user selecting all text and deleting it, which is more reliable across browsers. Just calling clear() may fail in some cases.