简体   繁体   English

Selenium Python Chromedriver更改文件下载路径

[英]Selenium Python Chromedriver Change File Download Path

I'm looking for a way to save different files to different locations in python using chromedriver. 我正在寻找一种方法,使用chromedriver将不同的文件保存到python中的不同位置。 The code below sets chrome to download to folder_path without pop the download location dialogue first. 下面的代码设置chrome以下载到folder_path而不首先弹出下载位置对话框。 After clicking and downloading one file into folder_path (I skipped pasting this part of code cause I have no issue), I want to download another file into new_folder_path. 单击并将一个文件下载到folder_path后(我跳过粘贴这部分代码导致我没有问题),我想将另一个文件下载到new_folder_path中。 But the code below gives me AttributeError: 'WebDriver' object has no attribute 'Chrome'. 但下面的代码给出了AttributeError:'WebDriver'对象没有属性'Chrome'。 Any ideas if I can change download location for Chrome under the same webdriver? 如果我可以在同一个webdriver下更改Chrome的下载位置,有什么想法吗?

folder_path = "C:\\Document"

def give_chrome_option(folder_path):
    chromeOptions = webdriver.ChromeOptions() #setup chrome option
    prefs = {"download.default_directory" : folder_path,
           "download.prompt_for_download": False,
           "download.directory_upgrade": True}  #set path
    chromeOptions.add_experimental_option("prefs", prefs) #set option
    return chromeOptions
driver = webdriver.Chrome(chrome_options = give_chrome_option(folder_path)
driver.get(sample_url)
driver.Chrome(chrome_options = give_chrome_option(new_folder_path))

No, you have to re-instantiate WebDriver if you want to download to a different directory. 不,如果要下载到其他目录,则必须重新实例化WebDriver。 Depending on what exactly do you need to do, a workaround described in the first answer here might be suitable for you (download to a temporary directory then move file using os.rename() ) 根据您需要做什么, 此处第一个答案中描述的解决方法可能适合您(下载到临时目录,然后使用os.rename()移动文件)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM