mm_
mm_

Reputation: 1735

Custom Download folder for chromedriver in python

I want to save the files I get with a scraper into custom folders. I looked around and none of the solutions I found worked for me. Here is my configuration:

options = webdriver.ChromeOptions()
prefs = {
        'profile.default_content_settings.popups': 0,
        'download.default_directory': my_data_folder,
        "download.directory_upgrade": True,
        "download.prompt_for_download": False,
        "safebrowsing.enabled":False,
        }
options.add_argument('--remote-debugging-port=9222') 
options.add_experimental_option("useAutomationExtension", False)

desired_caps = {
        'prefs': {
            'savefile': {
                'default_directory': my_data_folder,
                "directory_upgrade": True,
                "extensions_to_open": ""
            }
        }
    }


options.add_experimental_option('prefs', prefs)
driver = webdriver.Chrome(chrome_options=options, desired_capabilities=desired_caps)

But when I try downloading, it goes to ~/Downloads/ instead of my_data_folder. I have tried prefs and desired_caps independently to no avail.

I am using Chromium 108.0.5359.22 snap

Help is appreciated !

I have tried: How to download to a specific folder with Chromedriver?

Define download directory for chromedriver selenium with python

and many other posts and blogs. All these solutions are summarised in the script above.

Thanks !

UPDATE It works if I add

options.add_argument("--headless")

The folder option works, but this is not desirable for other reasons. Is there a better way to fix this problem?

Upvotes: 0

Views: 237

Answers (1)

Rihhard
Rihhard

Reputation: 66

Have you thought about using shutil to move the file after the download ?

Here's how I had that implemented in another project I was working on

filename = max([
    download_folder + "\\" + f for f in os.listdir(download_folder)],
    key=os.path.getctime)

shutil.move(
    filename,
    os.path.join(download_folder,f"filename.format")
)

Upvotes: 1

Related Questions