Home > Blockchain >  Selenium: Dropdown value selection from list
Selenium: Dropdown value selection from list

Time:08-06

I am using Python and Selenium to add certain information to a form.

In this case, I need to insert the city of residence, selecting it from an auto-complete drop-down menu.

Altough the list is dynamic and may change depending on other values, to illustrate, I would like to select "Roma".

How can I effectively find and select a value from a list that is dynamically changing, using Selenium?

I tried with the methodology explained After text addition

Snippets of the html code from the page are the following:

<input autocomplete="off" placeholder=" " id="dx_dx-e8acb639-2bdd-7dda-710a-2d81f408dc73_ComuneNascitaPk"  aria-haspopup="listbox" aria-autocomplete="list" type="text" spellcheck="false" tabindex="0" role="combobox" aria-expanded="false" aria-required="true">

From the expanded list:

<div  style="transform: translate(0px, 0px);"><div  role="option" aria-selected="false"><div >Affile</div></div><div  role="option" aria-selected="false"><div >Agosta</div></div><div  role="option" aria-selected="false"><div >Albano Laziale</div></div><div  role="option" aria-selected="false"><div >Allumiere</div></div><div  role="option" aria-selected="false"><div >Anguillara Sabazia</div></div><div  role="option" aria-selected="false"><div >Anticoli Corrado</div></div><div  role="option" aria-selected="false"><div >Anzio</div></div><div  role="option" aria-selected="false"><div >Arcinazzo Romano</div></div><div  role="option" aria-selected="false"><div >Ardea</div></div><div  role="option" aria-selected="false"><div >Ariccia</div></div><div  role="option" aria-selected="false"><div >Arsoli</div></div><div  role="option" aria-selected="false"><div >Artena</div></div><div  role="option" aria-selected="false"><div >Bellegra</div></div><div  role="option" aria-selected="false"><div >Bracciano</div></div><div  role="option" aria-selected="false"><div >Camerata Nuova</div></div><div  role="option" aria-selected="false"><div >Campagnano di Roma</div></div><div  role="option" aria-selected="false"><div >Canale Monterano</div></div><div  role="option" aria-selected="false"><div >Canterano</div></div><div  role="option" aria-selected="false"><div >Capena</div></div><div  role="option" aria-selected="false"><div >Capranica Prenestina</div></div><div  role="option" aria-selected="false"><div >Carpineto Romano</div></div><div  role="option" aria-selected="false"><div >Casape</div></div><div  role="option" aria-selected="false"><div >Castel Gandolfo</div></div><div  role="option" aria-selected="false"><div >Castel Madama</div></div><div  role="option" aria-selected="false"><div >Castel San Pietro Romano</div></div><div  role="option" aria-selected="false"><div >Castelnuovo di Porto</div></div><div  role="option" aria-selected="false"><div >Cave</div></div><div  role="option" aria-selected="false"><div >Cerreto Laziale</div></div><div  role="option" aria-selected="false"><div >Cervara di Roma</div></div><div  role="option" aria-selected="false"><div >Cerveteri</div></div><div  role="option" aria-selected="false"><div >Ciampino</div></div><div  role="option" aria-selected="false"><div >Ciciliano</div></div><div  role="option" aria-selected="false"><div >Cineto Romano</div></div><div  role="option" aria-selected="false"><div >Civitavecchia</div></div><div  role="option" aria-selected="false"><div >Civitella San Paolo</div></div><div  role="option" aria-selected="false"><div >Colleferro</div></div><div  role="option" aria-selected="false"><div >Colonna</div></div><div  role="option" aria-selected="false"><div >Fiano Romano</div></div><div  role="option" aria-selected="false"><div >Filacciano</div></div><div  role="option" aria-selected="false"><div >Fiumicino</div></div><div  role="option" aria-selected="false"><div >Fonte Nuova</div></div><div  role="option" aria-selected="false"><div >Formello</div></div><div  role="option" aria-selected="false"><div >Frascati</div></div><div  role="option" aria-selected="false"><div >Gallicano nel Lazio</div></div><div  role="option" aria-selected="false"><div >Gavignano</div></div><div  role="option" aria-selected="false"><div >Genazzano</div></div><div  role="option" aria-selected="false"><div >Genzano di Roma</div></div><div  role="option" aria-selected="false"><div >Gerano</div></div><div  role="option" aria-selected="false"><div >Gorga</div></div><div  role="option" aria-selected="false"><div >Grottaferrata</div></div><div  role="option" aria-selected="false"><div >Guidonia Montecelio</div></div><div  role="option" aria-selected="false"><div >Jenne</div></div><div  role="option" aria-selected="false"><div >Labico</div></div><div  role="option" aria-selected="false"><div >Ladispoli</div></div><div  role="option" aria-selected="false"><div >Lanuvio</div></div><div  role="option" aria-selected="false"><div >Lariano</div></div><div  role="option" aria-selected="false"><div >Licenza</div></div><div  role="option" aria-selected="false"><div >Magliano Romano</div></div><div  role="option" aria-selected="false"><div >Mandela</div></div><div  role="option" aria-selected="false"><div >Manziana</div></div><div  role="option" aria-selected="false"><div >Marano Equo</div></div><div  role="option" aria-selected="false"><div >Marcellina</div></div><div  role="option" aria-selected="false"><div >Marino</div></div><div  role="option" aria-selected="false"><div >Mazzano Romano</div></div><div  role="option" aria-selected="false"><div >Mentana</div></div><div  role="option" aria-selected="false"><div >Monte Compatri</div></div><div  role="option" aria-selected="false"><div >Monte Porzio Catone</div></div><div  role="option" aria-selected="false"><div >Monteflavio</div></div><div  role="option" aria-selected="false"><div >Montelanico</div></div><div  role="option" aria-selected="false"><div >Montelibretti</div></div><div  role="option" aria-selected="false"><div >Monterotondo</div></div><div  role="option" aria-selected="false"><div >Montorio Romano</div></div><div  role="option" aria-selected="false"><div >Moricone</div></div><div  role="option" aria-selected="false"><div >Morlupo</div></div><div  role="option" aria-selected="false"><div >Nazzano</div></div><div  role="option" aria-selected="false"><div >Nemi</div></div><div  role="option" aria-selected="false"><div >Nerola</div></div><div  role="option" aria-selected="false"><div >Nettuno</div></div><div  role="option" aria-selected="false"><div >Olevano Romano</div></div><div  role="option" aria-selected="false"><div >Palestrina</div></div><div  role="option" aria-selected="false"><div >Palombara Sabina</div></div><div  role="option" aria-selected="false"><div >Percile</div></div><div  role="option" aria-selected="false"><div >Pisoniano</div></div><div  role="option" aria-selected="false"><div >Poli</div></div><div  role="option" aria-selected="false"><div >Pomezia</div></div><div  role="option" aria-selected="false"><div >Ponzano Romano</div></div><div  role="option" aria-selected="false"><div >Riano</div></div><div  role="option" aria-selected="false"><div >Rignano Flaminio</div></div><div  role="option" aria-selected="false"><div >Riofreddo</div></div><div  role="option" aria-selected="false"><div >Rocca Canterano</div></div><div  role="option" aria-selected="false"><div >Rocca di Cave</div></div><div  role="option" aria-selected="false"><div >Rocca di Papa</div></div><div  role="option" aria-selected="false"><div >Rocca Priora</div></div><div  role="option" aria-selected="false"><div >Rocca Santo Stefano</div></div><div  role="option" aria-selected="false"><div >Roccagiovine</div></div><div  role="option" aria-selected="false"><div >Roiate</div></div><div  role="option" aria-selected="false"><div >Roma</div></div><div  role="option" aria-selected="false"><div >Roviano</div></div><div  role="option" aria-selected="false"><div >Sacrofano</div></div><div  role="option" aria-selected="false"><div >Sambuci</div></div><div  role="option" aria-selected="false"><div >San Cesareo</div></div><div  role="option" aria-selected="false"><div >San Gregorio da Sassola</div></div><div  role="option" aria-selected="false"><div >San Polo dei Cavalieri</div></div><div  role="option" aria-selected="false"><div >San Vito Romano</div></div><div  role="option" aria-selected="false"><div >Santa Marinella</div></div><div  role="option" aria-selected="false"><div >Sant'Angelo Romano</div></div><div  role="option" aria-selected="false"><div >Sant'Oreste</div></div><div  role="option" aria-selected="false"><div >Saracinesco</div></div><div  role="option" aria-selected="false"><div >Segni</div></div><div  role="option" aria-selected="false"><div >Subiaco</div></div><div  role="option" aria-selected="false"><div >Tivoli</div></div><div  role="option" aria-selected="false"><div >Tolfa</div></div><div  role="option" aria-selected="false"><div >Torrita Tiberina</div></div><div  role="option" aria-selected="false"><div >Trevignano Romano</div></div><div  role="option" aria-selected="false"><div >Vallepietra</div></div><div  role="option" aria-selected="false"><div >Vallinfreda</div></div><div  role="option" aria-selected="false"><div >Valmontone</div></div><div  role="option" aria-selected="false"><div >Velletri</div></div><div  role="option" aria-selected="false"><div >Vicovaro</div></div><div  role="option" aria-selected="false"><div >Vivaro Romano</div></div><div  role="option" aria-selected="false"><div >Zagarolo</div></div></div>

enter image description here

CodePudding user response:

You really can't use Select class since you're trying to handle div node not select node... You can use this approach to select reqired value

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

# Add code for clicking on input field to expand drop-down menu
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//div[@role='option' and .='Roma']"))).click()
  • Related