Anurag Sinha
Anurag Sinha

Reputation: 1

Trying to scavanage data using Selenium but save button isn't working

I am trying to save the booth addresses using Selenium for machine learning algorithms, but the save button is not getting clicked:

the floppy sign

I tried all types of selectors, different methods, JavaScript, and everything else, but it still doesn't work.

Here is the last code I tried.

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)

I want to download them as Excel recursively.

I want the Excel file to get downloaded. For that, the save icon should be clicked.

Please help, stuck for two days.

Upvotes: 0

Views: 49

Answers (1)

Kliment Merzlyakov
Kliment Merzlyakov

Reputation: 1083

This code worked for me. If it doesn't work, could you make sure that everything on the page was loaded and visible before you trying to download the data?

btn = self.driver.find_elements(By.XPATH, '//*[@title="Excel"]')
self.driver.execute_script ("arguments[0].click();", btn)

Upvotes: 0

Related Questions