Selenium Python中Element.click失效,无法生成CSV下载链接求助
解决NSE网站Selenium自动化下载CSV的问题
我完全懂你现在的 frustration——手动点Get Data就能出下载链接,自动化操作就掉链子,各种等待和点击方式都试了还是不行,甚至连输入的股票代码都好像没被页面认到,太磨人了!
问题根源分析
你提到输入"SBIN"后,print(driver.find_element_by_id("symbol").text)输出为空,这里其实有个小误区:输入框的内容存在value属性里,而不是元素的text节点,所以应该用.get_attribute('value')来验证输入是否成功。不过核心问题确实是页面的JavaScript验证逻辑没被触发——很多现代网站依赖input/change事件来检测输入变化,而Selenium的send_keys有时候不会自动触发这些事件,导致点击Get Data后,页面没有生成下载链接。
针对性解决方案
下面是修改后的完整脚本,我标注了关键改动点:
# -*- coding: utf-8 -*- from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.common.keys import Keys from selenium.webdriver.support.ui import Select, WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.common.exceptions import NoSuchElementException, NoAlertPresentException import unittest, time class TestNSEDownload(unittest.TestCase): def setUp(self): self.driver = webdriver.Firefox() self.driver.implicitly_wait(10) # 隐式等待缩短,配合显式等待更高效 self.verificationErrors = [] self.accept_next_alert = True def test_download_sbin_csv(self): driver = self.driver driver.get("https://www1.nseindia.com/products/content/equities/equities/eq_security.htm") # 选择数据类型(对应原代码的option[3],index从0开始所以是2) data_type_select = Select(driver.find_element(By.ID, "dataType")) data_type_select.select_by_index(2) # 处理股票代码输入,触发JS事件 symbol_input = driver.find_element(By.ID, "symbol") symbol_input.clear() symbol_input.send_keys("SBIN") # 手动触发input事件,让页面JS检测到内容变化 driver.execute_script("arguments[0].dispatchEvent(new Event('input'));", symbol_input) # 正确验证输入内容(用value属性) print("输入的股票代码:", symbol_input.get_attribute('value')) # 选择Series为EQ series_select = Select(driver.find_element(By.ID, "series")) series_select.select_by_visible_text("EQ") # 选择日期范围24 Months date_range_select = Select(driver.find_element(By.ID, "dateRange")) date_range_select.select_by_visible_text("24 Months") # 等待Get Data按钮可点击后再点击 get_data_btn = WebDriverWait(driver, 10).until( EC.element_to_be_clickable((By.ID, "get")) ) get_data_btn.click() # 等待下载链接出现并点击 download_link = WebDriverWait(driver, 20).until( EC.presence_of_element_located((By.LINK_TEXT, "Download file in csv format")) ) download_link.click() def is_element_present(self, how, what): try: self.driver.find_element(by=how, value=what) except NoSuchElementException as e: return False return True def is_alert_present(self): try: self.driver.switch_to.alert except NoAlertPresentException as e: return False return True def close_alert_and_get_its_text(self): try: alert = self.driver.switch_to.alert alert_text = alert.text if self.accept_next_alert: alert.accept() else: alert.dismiss() return alert_text finally: self.accept_next_alert = True def tearDown(self): self.driver.quit() self.assertEqual([], self.verificationErrors) if __name__ == "__main__": unittest.main()
关键改动说明
- 触发输入框的JS事件:通过
execute_script手动触发input事件,强制让页面的验证逻辑识别到股票代码已输入。 - 正确验证输入内容:用
get_attribute('value')代替.text来检查输入框的实际内容,避免误解输入是否成功。 - 优化等待策略:用
WebDriverWait配合element_to_be_clickable和presence_of_element_located,确保元素处于可交互状态后再操作,比固定等待更可靠。 - 简化Select操作:直接用
Select类处理下拉框,去掉多余的点击操作,避免干扰页面状态。 - 更新定位方式:改用Selenium 4推荐的
By.ID等定位语法,兼容新版本。
额外调试技巧
- 在关键步骤后添加
driver.save_screenshot("step.png"),查看页面状态是否符合预期。 - 打开浏览器的开发者工具(F12),在"Network"标签下监控请求,看点击Get Data后是否有API请求发送,帮助判断问题出在前端还是后端。
内容的提问来源于stack exchange,提问作者SujithHiremath
相关产品推荐
相关产品推荐

