Create a new folder and make it as default download in selenium

Viewed 1139

I am doing a selenium test in python where I want to create a new directory with current time and make it as default download folder. So whenever I run the script the default download location should be a new directory created at that time and the file should download there.

from selenium import webdriver
from datetime import datetime
import os

today = datetime.now()

current_dir = os.mkdir("/Users/Desktop/" + today.strftime('%Y-%m-%d_%H-%M-%S'))
browser = webdriver.Chrome('/Users/Desktop/chromedriver')
chromeOptions = webdriver.ChromeOptions()
prefs = {"download.default_directory" : current_dir }
chromeOptions.add_experimental_option("prefs",prefs)

I am running the above script and it creates a new folder but the default download location doesn't change as the file still downloads at chrome://Downloads. Is there any way we can change the new created folder as default download directory

3 Answers

The problem here is with the os.mkdir method. The os.mkdir() method in Python is used to create a directory named path with the specified numeric mode. This method does not return any value.

Execution of the code with debugging: enter image description here

If you look at the above image, you can see that the current_dir is of None type. So, the folder is created on the desktop, but the path to it is not being captured.

You need to create folder name and folder itself in two separate code lines:

current_dir_name = "/Users/Desktop/" + today.strftime('%Y-%m-%d_%H-%M-%S')
os.mkdir(current_dir_name)

and then path current_dir_name into

chromeOptions = webdriver.ChromeOptions()
prefs = {"download.default_directory" : current_dir_name}
chromeOptions.add_experimental_option("prefs", prefs)

To create a new directory with current time you can use the datetime module as follows:

  • Code Block:

    from datetime import datetime
    import os
    
    
    new_dir = "C:/Users/user-name/Desktop/" + datetime.now().strftime('%Y-%m-%d_%H-%M-%S')
    print(new_dir)
    if not os.path.exists(new_dir):
        os.makedirs(new_dir) 
    
  • Console Output:

    C:\Users\user-name\Desktop\Debanjan\PyPrograms>new_directory.py
    C:/Users/user-name/Desktop/2020-08-04_18-01-46
    
    C:\Users\user-name\Desktop\Debanjan\PyPrograms>new_directory.py
    C:/Users/user-name/Desktop/2020-08-04_18-02-01
    
    C:\Users\user-name\Desktop\Debanjan\PyPrograms>new_directory.py
    C:/Users/user-name/Desktop/2020-08-04_18-02-05
    
  • Snapshot of the newly created directories:

directories


This usecase

Now you can implement the same logic to create a new directory on each execution and set as the default download location using Selenium as follows:

from selenium import webdriver
from datetime import datetime
import os

new_dir = "C:/Users/user-name/Desktop/" + datetime.now().strftime('%Y-%m-%d_%H-%M-%S')
print(new_dir)
if not os.path.exists(new_dir):
    os.makedirs(new_dir) 

options = webdriver.ChromeOptions() 
options.add_experimental_option("prefs", {"download.default_directory" : new_dir})
driver = webdriver.Chrome(options=options, executable_path=r'C:\WebDrivers\chromedriver.exe')

Reference

You can find a couple of relevant detailed discussions in:

Related