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
JavaScript
x
21
21
1
driver = webdriver.Chrome("/Users/Hassan/Downloads/chromedriver")
2
driver.get("http://rgphentableaux.hcp.ma/Default1/")
3
4
# Click on "Langues locales utilisées"
5
langues_check = driver.find_element_by_xpath("//input[@value='5']")
6
driver.execute_script("arguments[0].click();", langues_check)
7
# Cliquer sur "Régions"
8
region_check = driver.find_element_by_xpath("//input[@value='Region']")
9
driver.execute_script("arguments[0].click();", region_check)
10
# Cliquer sur "Choisir une région"
11
choisir_region = driver.find_element_by_xpath("//input[@value='Choisir une entitée']")
12
driver.execute_script("arguments[0].click();", choisir_region)
13
14
#select region
15
#element = WebDriverWait(driver,5).until(EC.element_to_be_clickable((By.XPATH, "(//li[@class='active-result highlighted'])")))
16
element = driver.find_element_by_xpath(".//*[@data-option-array-index='0']")
17
driver.execute_script("arguments[0].click();", element)
18
19
# Click on "Afficher"
20
afficher_click = driver.find_element_by_xpath("//input[@value='Afficher']")
21
driver.execute_script("arguments[0].click();", afficher_click)
Advertisement
Answer
You can do like this
JavaScript
1
10
10
1
from selenium.webdriver.common.by import By
2
from selenium.webdriver.support.ui import WebDriverWait
3
from selenium.webdriver.support import expected_conditions as ec
4
5
options = WebDriverWait(driver, 10).until(ec.presence_of_all_elements_located((By.XPATH, "//ul[@class='chosen-results']/li[@class='active-result']")))
6
for option in options:
7
if option.text == 'option_you_want_to_click':
8
option.click()
9
break
10