Selenium + Python · Lesson 28 of 43

0/43 done

Module 28: Auto Complete & Dynamic Lists

28. Search Suggestions & Auto-Complete Lists

15 minlesson

Your task

Implement pick_matching_suggestion(items, keyword):

  1. Loop through items.
  2. If keyword in item.text, item.click() and return True.
  3. Return False if none matched.

Write it in the editor, then press Check answer (Ctrl + Enter).

Handling Auto-Complete Suggestions

python
search_box = driver.find_element(By.NAME, "search")
search_box.send_keys("Selenium")

# Wait for suggestions to populate:
items = WebDriverWait(driver, 5).until(
    EC.visibility_of_all_elements_located((By.CSS_SELECTOR, "ul.suggestions-list li"))
)

for item in items:
    if "Selenium Python" in item.text:
        item.click()
        break

Key Takeaways & Walkthrough Summary

Search bars with live suggestions load results via asynchronous AJAX queries. In this lesson, you will type text, wait for suggestions to render in the DOM, and select the desired item. Let's master autocomplete!

Part of the Selenium + Python course (Selenium with Python). Practice this concept live in the Selenium Online Compiler or explore our free automation practice form.

Common Doubts & Lesson Q&A

Why does finding suggestions right after send_keys return an empty list?

AJAX calls take a few hundred milliseconds. You must use WebDriverWait(EC.visibility_of_all_elements_located(...)) to synchronize.

Ready? Solve the task in the editor

Use Check answer to mark it complete. Hints are there if you need them.

Python
Skip