webdriver运行时如何更改默认的下载文件夹? [英] How to change default download folder while webdriver is running?
问题描述
设置Chrome首选项,具有Python中的Selenium Webdriver
问题是这些方法只允许我在打开webdriver时更改下载目录。需要一段时间才能到达下载页面,这样做是无效的解决方案。我已经尝试设置首选项,但我正在使用selenium webdriver和chrome在python,我还没有能够找到任何东西或在python帮助。即使在新的驱动程序上切换窗口句柄将无法正常工作,因为它无法抓住另一个驱动程序已打开的窗口。
下载网站的链接是自定义的,因此无法将其复制并粘贴到新的驱动程序中。到目前为止,我一直在使用os。模块来获取每个新文件的名称,但即使这样,由于下载次数不同,这也是不可靠的。
如果任何人有任何想法如何在webdriver运行时将默认设置更改为webdriver,这将是伟大的。谢谢!
过去,我已经通过下载到临时文件夹,然后将文件重命名为相应的文件夹有一些这样的东西:
def move_to_download_folder(downloadPath,newFileName,fileExtension):
got_file = False
##获取当前文件名。
while got_file = False:
try:
currentFile = glob.glob(DOWNLOAD_PATH +*+ fileExtension)
got_file = True
除了:
打印文件尚未完成下载
time.sleep(20)
##创建新文件名
fileDestination = downloadPath + newFileName + fileExtension
os.rename(currentFile,fileDestination)
返回
##单击元素下载文件
inputElement = driver.find_element_by_xpath({xpath here })。click()
move_to_download_folder(downloadPath,newFileName,fileExtension)
I am downloading several different data sets and would like each file (or set) to download to a specific folder. I have learned how to change the download directories at these page:
setting Chrome preferences w/ Selenium Webdriver in Python
Change the default chrome download folder webdriver C#
The problem is these methods only allow me to change the download directory when I open the webdriver. It takes a while to get to the download page so doing this is an ineffective solution. I've tried set preferences but I'm working with selenium webdriver and chrome in python and I have not been able to find anything on SO or in the python help. Even switching the window handle on a new driver won't work because it cannot grab another driver's already open window.
The link for the download site is customized so can't copy and paste into a new driver either. So far I've been using the os. module to get the name of each new file coming in but even this is unreliable because of varying download times.
If anyone has any idea on how to change the default settings to a webdriver while the webdriver is running that would be great. Thanks!
In the past, I have solved this by downloading to a temp folder and then renaming the file to the appropriate folder with something along the line of this:
def move_to_download_folder(downloadPath, newFileName, fileExtension):
got_file = False
## Grab current file name.
while got_file = False:
try:
currentFile = glob.glob(DOWNLOAD_PATH+"*"+fileExtension)
got_file = True
except:
print "File has not finished downloading"
time.sleep(20)
## Create new file name
fileDestination = downloadPath+newFileName+fileExtension
os.rename(currentFile, fileDestination)
return
## Click element to download file
inputElement=driver.find_element_by_xpath("{xpath here}").click()
move_to_download_folder(downloadPath, newFileName, fileExtension)
这篇关于webdriver运行时如何更改默认的下载文件夹?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!