0

I am trying to download a file with Selenium and python into a specific relative directory but I can't.

I tried the following codes that I have seen in the web:

options = webdriver.ChromeOptions()
options.add_argument("download.default_directory=/folder")
driver = webdriver.Chrome(chrome_options=options)

and this:

import webdriver
chrome_options = webdriver.ChromeOptions()
prefs = {'download.default_directory' : '/folder'}
chrome_options.add_experimental_option('prefs', prefs)
driver = webdriver.Chrome(chrome_options=chrome_options)

but they do not work. I also tried the following and it kind of works, but the select location window have to pop up, which I will have to manually click "Select"

from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument("--start-maximized")
prefs = {"profile.default_content_settings.popups": 0,
         "download.default_directory": 
                    r"C:\Users\user_dir\Desktop\\",#IMPORTANT - ENDING SLASH V IMPORTANT
         "directory_upgrade": True}
options.add_experimental_option("prefs", prefs)
browser=webdriver.Chrome(<chromdriver.exe path>, options=options)

Do anyone know why and how to fix it? Or are there any alternative solutions that allow me to know the directory of the file and access the file it just downloaded?

Julian Chu
  • 1,790
  • 2
  • 7
  • 12
  • 1
    Possible duplicate of [Selenium Webdriver in Python - files download directory change in Chrome preferences](https://stackoverflow.com/questions/18026391/selenium-webdriver-in-python-files-download-directory-change-in-chrome-prefere) – Infern0 Aug 19 '19 at 10:22
  • Yeah but all the answers there doesn't work for me – Julian Chu Aug 19 '19 at 23:42

1 Answers1

0

I wouldn't recommend using the browser for downloading content, leave it to Chrome developers/testers, instead you should rather get href attribute of the <a> element or src attribute for other elements you want to download and obtain it using for example requests library

If the website you're getting information from requires authentication you can obtain cookies from the browser and pass them to requests.Session object.

Dmitri T
  • 159,985
  • 5
  • 83
  • 133