如何在 webdriver 运行时更改默认下载文件夹? [英] How to change default download folder while webdriver is running?

查看:34
本文介绍了如何在 webdriver 运行时更改默认下载文件夹?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在下载几个不同的数据集,并希望将每个文件(或集)下载到特定文件夹.我已经学会了如何在这些页面更改下载目录:

在 Python 中使用 Selenium Webdriver 设置 Chrome 首选项

更改默认chrome下载文件夹webdriver C#

问题是这些方法只允许我在打开 webdriver 时更改下载目录.进入下载页面需要一段时间,所以这样做是一个无效的解决方案.我尝试过设置首选项,但我正在 python 中使用 selenium webdriver 和 chrome,但我无法在 SO 或 python 帮助中找到任何内容.即使在新驱动程序上切换窗口句柄也不起作用,因为它无法抓取另一个驱动程序已经打开的窗口.

下载站点的链接是自定义的,因此也无法复制并粘贴到新驱动程序中.到目前为止,我一直在使用 os.模块来获取每个新文件的名称,但由于下载时间不同,即使这样也不可靠.

如果有人知道如何在 webdriver 运行时将默认设置更改为 webdriver,那就太好了.谢谢!

解决方案

过去,我通过将文件下载到临时文件夹然后将文件重命名为适当的文件夹来解决此问题:

>

def move_to_download_folder(downloadPath, newFileName, fileExtension):got_file = 错误## 获取当前文件名.而 got_file = False:尝试:currentFile = glob.glob(DOWNLOAD_PATH+"*"+fileExtension)got_file = 真除了:打印文件尚未完成下载"时间.睡眠(20)##创建新文件名文件目的地 = 下载路径+新文件名+文件扩展名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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆