I am trying to download the daily report from the website NSE-India using selenium & python.
Approach to download the daily report
- Website loads with no data
- After X time,page is loaded with report information
- Once the page is loaded with report data,"table[@id='etfTable']" appears
- Explicit wait is added in the code,to wait till the "table[@id='etfTable']" loads
Code for explicit wait
element=WebDriverWait(driver,50).until(EC.visibility_of_element_located(By.xpath,"//table[@id='etfTable']"))
Extract the onclick event using xpath
downloadcsv= driver.find_element_by_xpath("//div[@id='esw-etf']/div[2]/div/div[3]/div/ul/li/a")
Trigger the click to download the file
Full code
from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC options =webdriver.ChromeOptions(); prefs={"download.default_directory":"/Volumes/Project/WebScraper/downloadData"}; options.binary_location=r'/Applications/Google Chrome 2.app/Contents/MacOS/Google Chrome' chrome_driver_binary =r'/usr/local/Caskroom/chromedriver/94.0.4606.61/chromedriver' options.add_experimental_option("prefs",prefs) driver =webdriver.Chrome(chrome_driver_binary,options=options) try: #driver.implicity_wait(10) driver.get('https://www.nseindia.com/market-data/exchange-traded-funds-etf') element =WebDriverWait(driver,50).until(EC.visibility_of_element_located(By.xpath,"//table[@id='etfTable']")) downloadcsv= driver.find_element_by_xpath("//div[@id='esw-etf']/div[2]/div/div[3]/div/ul/li/a") print(downloadcsv) downloadcsv.click() time.sleep(5) driver.close() except: print("Invalid URL")
Issue i am facing.
- The page is keeps on loading but when launched without selenium the daily report is getting loaded
- Not able to download the daily report