0
0
Selenium Javatesting~20 mins

Clearing fields in Selenium Java - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Clearing Fields 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 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?
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");
AField is empty
BField has text
CNullPointerException
DNoSuchElementException
Attempts:
2 left
💡 Hint
Remember what the clear() method does to the input field's value.
assertion
intermediate
2: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();
AassertNotNull(inputField.getText());
BassertTrue(inputField.getText().isEmpty());
CassertEquals("", inputField.getAttribute("value"));
DassertFalse(inputField.getAttribute("value").isEmpty());
Attempts:
2 left
💡 Hint
Check which method returns the current text inside an input field.
locator
advanced
2: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?
ABy.xpath("//input[@type='password']")
BBy.id("password")
CBy.cssSelector("input[name='pass']")
DBy.className("input-field")
Attempts:
2 left
💡 Hint
IDs are unique and fastest for locating elements.
🔧 Debug
advanced
2: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);
AsendKeys("query") after clear() is missing, so field remains empty.
Bclear() does not work on input fields, use sendKeys("") instead.
CThe locator By.id("search") is incorrect and finds no element.
DThe sendKeys(Keys.BACK_SPACE) adds a character after clearing, so field is not empty.
Attempts:
2 left
💡 Hint
Check what happens when you send a BACK_SPACE key after clearing.
framework
expert
3: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
}
A
field.sendKeys(Keys.CONTROL + "a");
field.sendKeys(Keys.DELETE);
field.sendKeys(text);
B
field.clear();
field.sendKeys(text);
C
field.sendKeys("");
field.clear();
field.sendKeys(text);
D
if (!field.getAttribute("value").isEmpty()) {
    field.clear();
}
field.sendKeys(text);
Attempts:
2 left
💡 Hint
Sometimes clear() alone does not work reliably on all browsers.