How to change default download folder while webdriver is running?

笑着哭i 提交于 2019-11-28 05:10:13

问题


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!


回答1:


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)


来源:https://stackoverflow.com/questions/23896625/how-to-change-default-download-folder-while-webdriver-is-running

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!