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

如何用选择器爬取网页?解决ogimet无value属性年份选项点击问题

Solution for Clicking Dropdown Options Without Value Attributes

Hey Javier, I’ve dealt with exactly this kind of dropdown issue in Selenium before—when options don’t have a value attribute, you just need to target them by their visible text instead. Here are a couple of straightforward approaches that work reliably:

The Select class is built specifically for handling dropdown menus, and it makes selecting options by text super easy—no need to mess with raw XPATH/CSS selectors. Here's how to integrate it into your code:

First, import the Select class at the top of your script:

from selenium.webdriver.support.ui import Select

Then, once you're on the page with the year dropdown, locate the dropdown element and use select_by_visible_text():

# Wait for the dropdown to load (you can replace time.sleep with WebDriverWait for better reliability)
time.sleep(2)

# Locate the year dropdown (update the selector to match the actual name/id of the year select element on ogimet)
year_dropdown = Select(driver.find_element_by_name('year'))  # Example: if the select has name="year"

# Select the desired year by its visible text
year_dropdown.select_by_visible_text('2004')

2. Directly Locate and Click the Option Element

If you prefer not to use the Select class, you can target the specific option element using its text content with an XPATH selector:

time.sleep(2)

# Locate the option with the exact text "2004" inside the year dropdown
year_option = driver.find_element_by_xpath("//select[@name='year']/option[text()='2004']")

# Click the option
year_option.click()

Notes for Reliability:

  • Replace [@name='year'] with the actual attribute of the year dropdown on ogimet (you can inspect the element to find its name, id, or other unique identifiers).
  • Instead of relying on time.sleep(), consider using WebDriverWait to wait for the element to be clickable—this makes your script more stable, especially if the page loads slowly:
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    from selenium.webdriver.common.by import By
    
    # Wait up to 10 seconds for the year option to be clickable
    year_option = WebDriverWait(driver, 10).until(
        EC.element_to_be_clickable((By.XPATH, "//select[@name='year']/option[text()='2004']"))
    )
    year_option.click()
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.07 20:52:29