让Python Selenium创建包含今天日期的文件夹,以保存下载的文件

kb5ga3dv  于 2022-12-29  发布在  Python
关注(0)|答案(1)|浏览(161)

你能让Python Selenium使用日期/时间来命名默认下载目录的文件夹吗?我现在就有这段代码,我想让它使用date.today()代替“date”来创建新文件夹

from datetime import date
today = date.today()

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

68de4m5k1#

这应该行得通:

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)

运行代码时,它将根据当前日期创建一个新文件夹,并将该文件夹用作selenium的下载目录。

相关问题