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

Selenium点击NSE证券页面Get Data按钮时浏览器挂死问题求助

问题根因分析

  • 直接用send_keys给日期输入框填值,没有触发该页面日期选择器绑定的校验、数据更新事件,页面认为未填写合法的日期参数,点击按钮后会触发内部逻辑阻塞导致浏览器挂死
  • 按钮元素加载完成后还需要等待可交互状态,直接调用click方法会出现element not interactable报错,或者触发异常逻辑

修复后代码

首先导入依赖的工具模块:

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

替换原有逻辑为以下代码:

# 打开目标页面
driver.get("https://www1.nseindia.com/products/content/equities/equities/eq_security.htm")
wait = WebDriverWait(driver, 15)

# 选择日期间隔单选框
wait.until(EC.element_to_be_clickable((By.ID, 'rdDateToDate'))).click()

# 输入股票代码
wait.until(EC.visibility_of_element_located((By.NAME, 'symbol'))).send_keys("TCS")

# 填写起始日期并触发变更事件,让页面识别输入内容
from_date = wait.until(EC.visibility_of_element_located((By.ID, 'fromDate')))
from_date.clear()
from_date.send_keys("11-04-2017")
driver.execute_script("arguments[0].dispatchEvent(new Event('change'))", from_date)

# 填写结束日期并触发变更事件
to_date = wait.until(EC.visibility_of_element_located((By.ID, 'toDate')))
to_date.clear()
to_date.send_keys("10-04-2018")
driver.execute_script("arguments[0].dispatchEvent(new Event('change'))", to_date)

# 等待按钮可点击,用JavaScript执行点击绕过交互限制
get_data_btn = wait.until(EC.element_to_be_clickable((By.ID, 'get')))
driver.execute_script("arguments[0].click();", get_data_btn)

关键修复点说明

  • 新增15秒显式等待,所有元素操作都会等元素加载到可交互状态再执行,避免element not interactable报错
  • 给日期输入框添加change事件触发逻辑,让页面的日期校验逻辑识别到输入的日期值,不会因为参数校验不通过卡住
  • 用JavaScript执行点击操作,不需要元素在视口中,也不会触发页面的异常交互阻塞逻辑,解决浏览器挂死的问题

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.03 04:24:03