Excel File is not downloaded using selenium we driver. Element is clicked but file isn't getting downloaded

I tried to download one excel file using the python selenium web-driver. After running the code I noticed that file is not downloaded (may be a source restriction). I am unable to figure out whether this can be handled or not. below is my code

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from webdriver_manager.chrome import ChromeDriverManager

ChromeDriver_Path = ChromeDriverManager().install()

options = Options()
options.binary_location = GoogleChrome_Path
options.add_argument("--headless")
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')
options.add_argument('--disable-extensions')
options.add_argument("--proxy-server='direct://'")
options.add_argument("--proxy-bypass-list=*")
options.add_argument('window-size=1920x1080')

Source_Export_Path = r'myfolderpath'
options.add_experimental_option("prefs", {"download.default_directory": Source_Export_Path,
                                          "download.prompt_for_download": False,
                                          "download.directory_upgrade": True,
                                          "safebrowsing.enabled": True
                                          })
Source_Url = 'https://www.rbnz.govt.nz/statistics/j10-insurance-income-statement?__cf_chl_jschl_tk__=GXpMiuooASSLEEV4GrC0ODNHt3tzhq3PE2pXI_hoUsw-1641891789-0-gaNycGzNC6U'
driver = webdriver.Chrome(executable_path= ChromeDriver_Path, options = options)
driver.get(Source_Url)

element_1 = driver.find_element_by_css_selector(
    '#RbnzContent > div.table.summaryinfo-table > div:nth-child(5) > div.summaryinfo-data.col-lg-8.col-md-9.col-sm-12.col-xs-12 > a:nth-child(2)'
    ).click()

but when I check my directory I see that there are no files downloaded. I even checked the selenium browser window and I noticed that website isn't letting python script to download the file using automated way. Is there any way to fix this issue!!.

This is the url:


Solution 1:

try adding a delay / wait before clicking the element.
Expected conditions explicit wait is the preferred way to do that.
Also, you could improve the locator.
Also in case the code you shared is all your code you can add a delay after the clicking on the download button otherwise the session will close immediately after the click so file will be not downloaded.
Let me know if this worked better:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from webdriver_manager.chrome import ChromeDriverManager

ChromeDriver_Path = ChromeDriverManager().install()

options = Options()
options.binary_location = GoogleChrome_Path
options.add_argument("--headless")
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')
options.add_argument('--disable-extensions')
options.add_argument("--proxy-server='direct://'")
options.add_argument("--proxy-bypass-list=*")
options.add_argument('window-size=1920x1080')

Source_Export_Path = r'myfolderpath'
options.add_experimental_option("prefs", {"download.default_directory": Source_Export_Path,
                                          "download.prompt_for_download": False,
                                          "download.directory_upgrade": True,
                                          "safebrowsing.enabled": True
                                          })
Source_Url = 'https://www.rbnz.govt.nz/statistics/j10-insurance-income-statement?__cf_chl_jschl_tk__=GXpMiuooASSLEEV4GrC0ODNHt3tzhq3PE2pXI_hoUsw-1641891789-0-gaNycGzNC6U'
driver = webdriver.Chrome(executable_path= ChromeDriver_Path, options = options)
wait = WebDriverWait(driver, 20)

driver.get(Source_Url)
wait.until(EC.visibility_of_element_located((By.XPATH, "//a[contains(@href,'Statistics') and(contains(text(),'Insurance'))]"))).click()
time.sleep(10)