'Get text of drop-down menu

I have a drop-down menu.

I want to get all cities from A-Z using Python's Selenium.

<li> is a list of every city starting with an A.

But how to get the text now? My proceeding:

# Open drop-down menu
driver.find_element(BY.XPATH, '//*[@id="city-field"]/div/div/div[1]/button').click()

# Type in 'A'
driver.find_element(BY.XPATH, '//*[@id="city-field"]/div/div/div[1]/div/div[1]/input').send_keys('A')

# Get every cities starting with 'A' ???

enter image description here enter image description here



Solution 1:[1]

Something like this should work. It would've better if you provided the code instead of image, or the website link would've been even better to investigate. Anyway, try like this:

options = driver.find_elements(By.XPATH, "//div[@class='dropdown-menu open']//*[@role='option]"

ls = [option.text for option in options]
print(ls)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Anand Gautam