Skip to content
Advertisement

Pb to select a text from a dropdown list

I started learning Python/Selenium.

After several attempts, I can’t find a way to extract the text from a dropdown list, and I would like to put a for loop after the first extraction.

this is the image: enter image description here

the link : enter link description here

driver = webdriver.Chrome("/Users/Hassan/Downloads/chromedriver")
driver.get("http://rgphentableaux.hcp.ma/Default1/")

# Click on "Langues locales utilisées"
langues_check = driver.find_element_by_xpath("//input[@value='5']")
driver.execute_script("arguments[0].click();", langues_check)
# Cliquer sur "Régions"
region_check = driver.find_element_by_xpath("//input[@value='Region']")
driver.execute_script("arguments[0].click();", region_check)
# Cliquer sur "Choisir une région"
choisir_region = driver.find_element_by_xpath("//input[@value='Choisir une entitée']")
driver.execute_script("arguments[0].click();", choisir_region)

#select region
#element = WebDriverWait(driver,5).until(EC.element_to_be_clickable((By.XPATH, "(//li[@class='active-result highlighted'])")))
element = driver.find_element_by_xpath(".//*[@data-option-array-index='0']")
driver.execute_script("arguments[0].click();", element)

# Click on "Afficher"
afficher_click = driver.find_element_by_xpath("//input[@value='Afficher']")
driver.execute_script("arguments[0].click();", afficher_click)

Advertisement

Answer

You can do like this

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

options = WebDriverWait(driver, 10).until(ec.presence_of_all_elements_located((By.XPATH, "//ul[@class='chosen-results']/li[@class='active-result']")))
for option in options:
    if option.text == 'option_you_want_to_click':
        option.click()
        break
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement