简体   繁体   English

铅到 select 下拉列表中的文本

[英]Pb to select a text from a dropdown list

I started learning Python/Selenium.我开始学习 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.经过几次尝试,我找不到从下拉列表中提取文本的方法,我想在第一次提取后放置一个 for 循环。

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)

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM