Bird
0
0

Given the following code snippet, what will be the value of the input field after execution?

medium📝 Predict Output Q13 of 15
Selenium Java - Handling Form Elements
Given the following code snippet, what will be the value of the input field after execution?
driver.findElement(By.id("searchBox")).sendKeys("App");
new WebDriverWait(driver, Duration.ofSeconds(5))
  .until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("ul.suggestions li")));
List options = driver.findElements(By.cssSelector("ul.suggestions li"));
for (WebElement option : options) {
  if (option.getText().equals("Apple")) {
    option.click();
    break;
  }
}
String value = driver.findElement(By.id("searchBox")).getAttribute("value");
A"App"
B"Apple"
C"" (empty string)
DThrows NoSuchElementException
Step-by-Step Solution
Solution:
  1. Step 1: Analyze input and wait

    The code types "App" and waits for suggestions to appear.
  2. Step 2: Select matching suggestion

    It loops through suggestions and clicks the one with text "Apple".
  3. Step 3: Check input value after click

    Clicking "Apple" updates the input field value to "Apple".
  4. Final Answer:

    "Apple" -> Option B
  5. Quick Check:

    Selected suggestion text = input value [OK]
Quick Trick: Click matching suggestion updates input value [OK]
Common Mistakes:
MISTAKES
  • Assuming input stays as typed text
  • Ignoring wait for suggestions
  • Expecting empty input after click

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Selenium Java Quizzes