Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Making Python Selenium create folder with today's date to save downloaded files in

Can you make Python Selenium name folder that is default download directory using date/time?
I have this code right now and I wanted it to make new folder using date.today() in place of "date"

from datetime import date
today = date.today()

prefs = {'download.default_directory' : 'E:\\selenium_test\\date\\'}
options.add_experimental_option('prefs', prefs)

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

This should work:

import os
from selenium import webdriver
from datetime import datetime

# Set the path for the parent folder
path = "E:\\selenium_test\\date\\"

# Get the current date
now = datetime.now()

# Create the folder name using the current date
folder_name = f"{now.year}-{now.month}-{now.day}"

# Create the full path for the folder
full_path = os.path.join(path, folder_name)

# Create the folder
os.mkdir(full_path)

# Set the download directory for Selenium
options = webdriver.ChromeOptions()
prefs = {'download.default_directory': full_path}
options.add_experimental_option('prefs', prefs)

# Create the Selenium webdriver with the options
driver = webdriver.Chrome(options=options)

When the code is run, it will create a new folder based on the current date and use that folder as a download directory for the selenium.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading