0

I am trying to download an excel file using Selenium in Python from a website I need the file to be downloaded in the current folder instead of "download"

but it is not working, it is downloading it in the downloads folder

mime_types = [
    'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
]
options = Options()
options.set_preference("browser.download.folderList",2)
options.set_preference("browser.download.manager.showWhenStarting", False)
options.set_preference("browser.download.dir","./")
options.set_preference("browser.helperApps.neverAsk.saveToDisk", ",".join(mime_types))

s = Service(GeckoDriverManager().install())
driver = webdriver.Firefox(service=s, options=options)

driver.get("https://chartink.com/screener/close-below-bb-205")
WebDriverWait(driver, 2).until(EC.element_to_be_clickable((By.XPATH, "/html/body/div[2]/div[2]/div[2]/div/div/div/div[2]/div/div[2]/div[6]/div[1]/div/div[1]/div/button[3]"))).click()

please let me know what is wrong with the code?

Ashish
  • 479
  • 3
  • 7
  • 18

1 Answers1

1

To download a file using Selenium in Python within a specified directory you need to tweak the following about:config entries:

browser_download_dir


Solution

So an working solution can be to create a FirefoxProfile and then create a new directory to later download the files in it as follows:

newpath = 'C:\\home\\vivvin\\shKLSE'
if not os.path.exists(newpath):
    os.makedirs(newpath)    

profile = webdriver.FirefoxProfile()
profile.set_preference("browser.download.dir",newpath);
profile.set_preference("browser.download.folderList",2);

Reference

You can find a couple of detailed discussion in:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352