Bird
0
0

Identify the error in the following Selenium Java code for selecting an auto-complete option:

medium📝 Debug Q14 of 15
Selenium Java - Handling Form Elements
Identify the error in the following Selenium Java code for selecting an auto-complete option:
driver.findElement(By.id("searchBox")).sendKeys("Ban");
List options = driver.findElements(By.cssSelector("ul.suggestions li"));
for (WebElement option : options) {
  if (option.getText().equals("Banana")) {
    option.click();
  }
}
AMissing explicit wait for suggestions to appear before finding elements
BUsing sendKeys instead of setText method
CIncorrect CSS selector syntax for suggestions list
DNot clearing the input field before typing
Step-by-Step Solution
Solution:
  1. Step 1: Check for wait usage

    The code finds suggestions immediately after typing without waiting for them to appear.
  2. Step 2: Consequence of missing wait

    Without explicit wait, options list may be empty causing no click action.
  3. Final Answer:

    Missing explicit wait for suggestions to appear before finding elements -> Option A
  4. Quick Check:

    Wait before findElements = Missing explicit wait for suggestions to appear before finding elements [OK]
Quick Trick: Always wait for suggestions before accessing them [OK]
Common Mistakes:
MISTAKES
  • Skipping explicit wait causing empty list
  • Assuming suggestions load instantly
  • Not breaking loop after click

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Selenium Java Quizzes