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

Question:

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)
Asked By: Aeternus

||

Answers:

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.

Answered By: TrimPeachu