Bird
0
0

Find the bug in this code snippet for selecting the second auto-complete suggestion:

medium📝 Debug Q7 of 15
Selenium Java - Handling Form Elements
Find the bug in this code snippet for selecting the second auto-complete suggestion:
driver.findElement(By.id("input")).sendKeys("car");
new WebDriverWait(driver, Duration.ofSeconds(10))
  .until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector(".suggestions li")));
List items = driver.findElements(By.cssSelector(".suggestions li"));
items.get(1).click();
AIndexOutOfBoundsException if less than 2 suggestions
BNo bug; code works correctly
CWrong locator for suggestions list
DMissing sendKeys call
Step-by-Step Solution
Solution:
  1. Step 1: Analyze list access

    items.get(1) accesses second element; if fewer than 2 suggestions, this causes IndexOutOfBoundsException.
  2. Step 2: Importance of checking list size

    Code should verify list size before accessing index to avoid runtime errors.
  3. Final Answer:

    IndexOutOfBoundsException if less than 2 suggestions -> Option A
  4. Quick Check:

    Check list size before accessing index [OK]
Quick Trick: Always check list size before accessing by index [OK]
Common Mistakes:
  • Assuming fixed number of suggestions
  • Not handling empty or small lists
  • Ignoring runtime exceptions

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Selenium Java Quizzes