如何在 webdriver 运行时更改默认下载文件夹?

see*_*spi 6 python selenium google-chrome download preferences

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

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

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

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

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

如果有人对如何在 webdriver 运行时将默认设置更改为 webdriver 有任何想法,那就太好了。谢谢!

Exp*_*ode 5

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

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)
Run Code Online (Sandbox Code Playgroud)