pythonselenium-webdrivernse

Issue in downloading pre open market file from NSE website using Selenium


I am trying to download the pre open market from NSE website using Selenium.

url = 'https://www.nseindia.com/market-data/pre-open-market-cm-and-emerge-market'

I am trying to download the Category 'All' data.

url = 'https://www.nseindia.com/market-data/pre-open-market-cm-and-emerge-market'
driver = webdriver.Chrome()
driver.get(url)
element_select = driver.find_element(By.ID,'sel-Pre-Open-Market')
element = Select(element_select)
element.select_by_visible_text('All') # select All
time.sleep(10)
driver.find_element(By.ID,'downloadPreopen').click() # click Download

I could see the Category list is changing to 'All' but data is not being downloaded. Would appreciate guidance and also if there is any way to control the downloaded file.


Solution

  • Your code should work. You have to wait for the download to be started. There is a notification for download started. Wait for that notification to show up.

    Also, use explicit wait instead of fixed sleep. It will ensure the data is loaded after selection of the option.

    Try the following.

    from selenium import webdriver
    from selenium.webdriver.common.by import By
    from selenium.webdriver.chrome.options import Options
    from selenium.webdriver.support.select import Select
    from selenium.webdriver.support.wait import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    
    # Set up Chrome options 
    chrome_options = Options()
    chrome_options.add_argument('--start-maximized')
    chrome_options.add_argument("--disable-blink-features=AutomationControlled") # Avoid getting recognized as selenium controlled
    
    chrome_options.add_argument("--user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36")  # Set user agent
    chrome_options.add_experimental_option( "prefs", { "download.default_directory": "C:\\Users\\Download\\Path" }) # Set Download location
    
    
    driver = webdriver.Chrome(options=chrome_options)
    wait = WebDriverWait(driver, 60)
    
    URL = "https://www.nseindia.com/market-data/pre-open-market-cm-and-emerge-market"
    
    driver.get(URL)
    
    
    element_select = driver.find_element(By.ID,'sel-Pre-Open-Market')
    element = Select(element_select)
    element.select_by_visible_text('All') # select All
    wait.until(EC.presence_of_all_elements_located((By.CSS_SELECTOR,'#livePreTable tbody > tr'))) # wait for the table to be populated
    driver.find_element(By.ID,'downloadPreopen').click() # click Download
    
    wait.until(EC.presence_of_element_located((By.CSS_SELECTOR,'#error-snackbar.notError.show')))
    wait.until(EC.invisibility_of_element_located((By.CSS_SELECTOR,'#error-snackbar.notError.show')))
    
    driver.quit()
    

    Update

    I have added the preference in the chrome option to change the download path. The site detects selenium and so used options to avoid that.