我有一个脚本,该脚本使用selenium和webdriver作为Chrome自动下载文件 基本上,它登录到工作网站,然后单击一些设置以准备下载报告文件,然后单击“下载”按钮
Selenium或任何其他库是否可以获取正在下载或刚刚下载的文件的文件位置和名称,以便将其存储在变量中以供以后在脚本中使用
我不知道相对路径是否有效,或者是否需要使用完整的Windows路径名称才能起作用...可能可以肯定地假设完整路径可以正常工作
示例为C:\ Users \ FunnyUserName \ Downloads \ report.xls
添加代码以显示正在发生的事情
#For Report Pull
#-----------------------------------------------
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import datetime
import os
import glob
###############################################################
#Pull Report #
###############################################################
#Open Web Driver
browser = webdriver.Chrome()
#Open Website and Log in
print('Open Website and Log In')
browser.get(('https://SomeWebsite.com'))
print('Working on Getting the QC Report')
print('Please Stand By')
#####
#I Removed a lot of stuff not necessary to this question
#Get the File
WebDriverWait(browser,10).until(EC.element_to_be_clickable((By.XPATH,'//*[@id="btnGenerateReport"]'))).click()
time.sleep(4)
#Working on getting the last downloaded Filename
# get the user download folder (dynamic so will work on any machine)
downLoadFolder =os.path.join( os.getenv('USERPROFILE'), 'Downloads')
print(downLoadFolder)
#This shows the correct folder....
#In My Case C:\Users\My UserName\Downloads
# get the list of files
list_of_files = glob.glob(downLoadFolder+"/*.*") # * means all if need specific formats (if you are looking for any specific format then specify eg: "/*.xls" to filter)
print (list_of_files)
#Always Shows ['C:\\Users\\My UserName\\Downloads\\desktop.ini']
# get the latest file name
#Forced the Folder and file type as a test
latest_file = max(glob.glob("C:/Users/My Username/Downloads/*.xls"), key=os.path.getctime)
#print the latest file name
print(latest_file)
#Returns:latest_file = max(glob.glob("C:/Users/My Username/Downloads/*.xls"), key=os.path.getctime)
#ValueError: max() arg is an empty sequence
答案 0 :(得分:0)
这是python中的解决方案。
需要进口:
import glob
import os
脚本:
# get the user download folder (dynamic so will work on any machine)
downLoadFolder =os.path.join( os.getenv('USERPROFILE'), 'Downloads')
# get the list of files
list_of_files = glob.glob(downLoadFolder+"/*") # * means all if need specific formats (if you are looking for any specific format then specify eg: "/*.xlsx" to filter)
# get the latest file name
latest_file = max(list_of_files, key=os.path.getctime)
#print the latest file name
print(latest_file)
答案 1 :(得分:0)
我可能已经找出问题所在了
我需要它来获取实际的下载目录,而不是系统默认目录
我正在家用计算机上进行测试,它默认会下载到我与工作计算机共享的Dropbox下载目录中
因此它不是C:\ Users \ My Username \ Downloads \
实际上是D:\ Dropbox \ Downloads ...,这是我当前设置的Chrome默认设置
我如何获得chrome下载目录?