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

求助:如何使用Selenium在港交所港股通指定页面选择日期?

Fixing HKEX Date Picker with Selenium

Hey there, I’ve wrestled with fintech site date pickers like HKEX’s before—they often use custom widgets that ignore standard Selenium send_keys() calls, so it’s no surprise the usual SO solutions didn’t cut it. Let’s break down two reliable approaches for this specific page:

Approach 1: Inject Date via JavaScript (Fastest Method)

Most custom date pickers are tied to an underlying <input> element, even if it’s styled to look like a fancy widget. We can bypass the visual picker entirely by directly setting the input’s value and triggering the required change event.

Here’s the Python code:

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

# Initialize your driver (Chrome example)
driver = webdriver.Chrome()
driver.get("https://www.hkex.com.hk/Mutual-Market/Stock-Connect/Statistics/Historic-Daily?sc_lang=en")

# Wait for the date input to load (adjust XPath if needed)
date_input = WebDriverWait(driver, 15).until(
    EC.presence_of_element_located((By.XPATH, "//input[@data-testid='date-picker-input']"))
)

# Set your target date (format: DD/MM/YYYY)
target_date = "01/01/2023"
driver.execute_script(f"arguments[0].value = '{target_date}';", date_input)

# Trigger a change event to let the page recognize the new date
driver.execute_script("arguments[0].dispatchEvent(new Event('change'));", date_input)

# Optional: Click the "Search" button to apply the date
search_btn = WebDriverWait(driver, 10).until(
    EC.element_to_be_clickable((By.XPATH, "//button[contains(text(), 'Search')]"))
)
search_btn.click()

Approach 2: Interact with the Visual Date Picker

If the JS method fails (some sites block direct input manipulation), you can interact with the actual calendar widget step-by-step:

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

driver = webdriver.Chrome()
driver.get("https://www.hkex.com.hk/Mutual-Market/Stock-Connect/Statistics/Historic-Daily?sc_lang=en")

# 1. Click the date input to open the calendar
date_input = WebDriverWait(driver, 15).until(
    EC.element_to_be_clickable((By.XPATH, "//input[@data-testid='date-picker-input']"))
)
date_input.click()

# 2. Select the target year (adjust the text match to your year)
year_selector = WebDriverWait(driver, 10).until(
    EC.element_to_be_clickable((By.XPATH, "//div[contains(@class, 'rdtYear')]"))
)
year_selector.click()
target_year = WebDriverWait(driver, 10).until(
    EC.element_to_be_clickable((By.XPATH, "//td[text()='2023']"))
)
target_year.click()

# 3. Select the target month (e.g., "Jan" for January)
target_month = WebDriverWait(driver, 10).until(
    EC.element_to_be_clickable((By.XPATH, "//td[text()='Jan']"))
)
target_month.click()

# 4. Select the target day
target_day = WebDriverWait(driver, 10).until(
    EC.element_to_be_clickable((By.XPATH, "//td[text()='1']"))
)
target_day.click()

# 5. Submit the search
search_btn = WebDriverWait(driver, 10).until(
    EC.element_to_be_clickable((By.XPATH, "//button[contains(text(), 'Search')]"))
)
search_btn.click()

Pro Tips:

  • Always use WebDriverWait instead of time.sleep()—it’s more reliable and efficient.
  • Use your browser’s DevTools (F12) to inspect the date picker elements: check for unique attributes like data-testid or class names to refine your XPath/CSS selectors.
  • If the page uses iframes (unlikely here), you’ll need to switch to the iframe before interacting with elements.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 09:02:03