You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何遍历下拉列表并点击每个选项?下拉列表ID动态变化

How to Iterate and Click Each Option in a Dynamic-ID Dropdown

Since your dropdown has a dynamic ID (like id1f which changes), you can’t rely on the ID to locate it. Instead, use stable attributes like its name ("orgSelection") or class ("select150") to target the element. Below’s a step-by-step solution using Selenium (a popular web automation tool) with Python, which handles the form submission and page reloads triggered when you select an option.

Step 1: Locate the Dropdown with a Stable Selector

Skip the dynamic ID and use one of these reliable selectors:

  • By name: By.NAME, "orgSelection" (most specific here, since the name attribute is fixed)
  • By class + CSS selector: By.CSS_SELECTOR, "select.select150"

Step 2: Collect Option Values First (Avoid Stale Elements)

When you click an option, the form submits and the page reloads. This makes the original dropdown element "stale" (no longer attached to the DOM). To avoid errors, first collect all non-empty option values, then loop through them to select each one.

Full Example Code

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

# Initialize the browser driver (Chrome in this case)
driver = webdriver.Chrome()
driver.get("YOUR_PAGE_URL_HERE")  # Replace with your actual page URL

try:
    # Wait for the dropdown to load (explicit waits prevent race conditions)
    dropdown = WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.NAME, "orgSelection"))
    )
    
    # Collect all non-empty option values
    options = dropdown.find_elements(By.TAG_NAME, "option")
    option_values = [opt.get_attribute("value") for opt in options if opt.get_attribute("value") != ""]
    
    # Iterate through each option value
    for value in option_values:
        # Re-locate the dropdown (page reloads after each click, so elements refresh)
        dropdown = WebDriverWait(driver, 10).until(
            EC.presence_of_element_located((By.NAME, "orgSelection"))
        )
        
        # Use Selenium's Select class to pick the option by value
        select = Select(dropdown)
        select.select_by_value(value)
        
        # Wait for the form to submit and the page to navigate
        WebDriverWait(driver, 10).until(EC.url_changes(driver.current_url))
        
        # --- Add your custom logic here ---
        # For example: extract data from the new page, take a screenshot, etc.
        
        # Navigate back to the original page to select the next option
        driver.back()
        # Wait for the original page to reload fully
        WebDriverWait(driver, 10).until(
            EC.presence_of_element_located((By.NAME, "orgSelection"))
        )
        
finally:
    # Clean up: close the browser when done
    driver.quit()

Key Notes:

  • Explicit Waits: Used instead of implicitly_wait to ensure elements are fully loaded before interacting, which reduces errors.
  • Select Class: Selenium’s Select class simplifies working with standard <select> elements—you can also use select_by_visible_text() if you prefer targeting options by their display text (e.g., "MCH - Cardiovascular Surgery").
  • Handling Page Reloads: After each click, we wait for the URL to change (confirming the form submitted), then navigate back to the original page to select the next option.
  • Alternative Selectors: If the name attribute ever changes, switch to the CSS selector (By.CSS_SELECTOR, "select.select150")—this targets any <select> element with the class select150.

If you’re using a different tool (like Playwright, Cypress, or JavaScript), the core logic stays the same: locate the dropdown via a stable attribute, collect the options, then iterate and select each one while handling page navigation.

内容的提问来源于stack exchange,提问作者Dimple Mathew

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 06:35:36