【问题标题】:Pb to select a text from a dropdown listPb 从下拉列表中选择文本
【发布时间】:2020-12-25 17:07:29
【问题描述】:

我开始学习 Python/Selenium。

经过几次尝试,我找不到从下拉列表中提取文本的方法,我想在第一次提取后放置一个 for 循环。

这是图片: enter image description here

链接: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)

【问题讨论】:

    标签: python selenium web web-scraping beautifulsoup


    【解决方案1】:

    你可以这样做

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-03
      • 2017-05-19
      相关资源
      最近更新 更多