尝试使用 Selenium 清理数据,但保存按钮不起作用

问题描述 投票:0回答:1

我正在尝试使用机器学习算法的硒来保存展位地址,但保存按钮没有被单击。

在此处输入图像描述(软盘标志)

我尝试了所有类型的选择器、不同的方法、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.x, "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。

我希望下载 Excel 文件,以便单击保存图标。 请帮助卡住两天了。0

selenium-webdriver data-science selenium-ide data-science-experience
1个回答
0
投票

这段代码对我有用。如果不起作用,您能否在尝试下载数据之前确保页面上的所有内容均已加载且可见?

btn = self.driver.find_elements(By.XPATH, '//*[@title="Excel"]')
self.driver.execute_script ("arguments[0].click();", btn)
© www.soinside.com 2019 - 2024. All rights reserved.