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

使用Selenium批量下载Excel时保存按钮点击失效求助

无法通过Selenium点击ReportViewer导出按钮(软盘图标)下载Excel数据

我正尝试使用Selenium下载booth地址数据用于机器学习算法,但页面的保存按钮(软盘标识)无法被点击:
软盘图标

已尝试所有类型的选择器、不同点击方法、JavaScript点击等,但仍然无效。以下是我最后尝试的代码:

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

class Download():
    def setup_method(self, method):
        self.driver = webdriver.Edge()
        self.vars = {}
        self.wait = WebDriverWait(self.driver, 10)  # Adjust timeout as needed
    
    def teardown_method(self, method):
        self.driver.quit()
    
    def wait_for_window(self, timeout = 2):
        time.sleep(timeout / 1000)
        wh_now = self.driver.window_handles
        wh_then = self.vars["window_handles"]
        if len(wh_now) > len(wh_then):
            return set(wh_now).difference(set(wh_then)).pop()
    
    def download_all(self):
        self.driver.get("https://ceoelection.maharashtra.gov.in/SearchInfo/ListPSs.aspx")
        self.driver.set_window_size(1248, 835)
        
        # Get all districts
        district_select = Select(self.driver.find_element(By.ID, "ctl00_content_DistrictList"))
        districts = [option.text for option in district_select.options if option.text != '-- Select District --']
        
        for district in districts:
            # Select district
            district_select.select_by_visible_text(district)
            
            # Wait for assembly dropdown to be visible and enabled
            self.wait.until(EC.visibility_of_element_located((By.ID, "ctl00_content_AssemblyList")))
            assembly_select = Select(self.driver.find_element(By.ID, "ctl00_content_AssemblyList"))
            
            # Get all assembly constituencies for the selected district
            assemblies = [option.text for option in assembly_select.options if option.text != '-- Select Assembly --']
            
            for assembly in assemblies:
                # Select assembly constituency
                assembly_select.select_by_visible_text(assembly)
                
                # Wait for language selection to be clickable
                self.wait.until(EC.element_to_be_clickable((By.ID, "ctl00_content_LangList_1")))
                
                # Select the language (assuming "English" is the language to be selected)
                self.driver.find_element(By.ID, "ctl00_content_LangList_1").click()
                
                # Wait for generate report button to be clickable
                try:
                    element = self.driver.find_element(By.ID, "ctl00_content_ReportViewer1_ctl05_ctl04_ctl00_ButtonLink")
                    self.driver.execute_script("arguments[0].click();", element)
                # also tried
                # self.driver.find_element(By.ID, "ctl00_content_ReportViewer1_ctl05_ctl04_ctl00_ButtonImg").click()
                except Exception as e:
                    print(f"Error clicking export dropdown: {e}")

                # Wait for the Excel download link to be visible
                # self.wait.until(EC.visibility_of_element_located((By.LINK_TEXT, "Excel")))
                
                # Click to download the Excel file
                time.sleep(5)
                self.vars["window_handles"] = self.driver.window_handles
                self.driver.find_element(By.LINK_TEXT, "Excel").click()

                self.vars["win2902"] = self.wait_for_window(2000)
                self.vars["root"] = self.driver.current_window_handle
                self.driver.switch_to.window(self.vars["win2902"])
                self.driver.close()
                self.driver.switch_to.window(self.vars["root"])

                # Optional: wait for a bit before proceeding to the next download
                time.sleep(1)

if __name__ == "__main__":
    dl = Download()
    dl.setup_method(None)
    try:
        dl.download_all()
    finally:
        dl.teardown_method(None)

我想要递归下载这些数据为Excel文件,需成功点击保存图标完成下载,已卡两天,恳请帮助。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.20 18:33:13