使用Selenium和python将文件下载到指定位置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25251583/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Downloading file to specified location with Selenium and python
提问by Jerad Bill
Ok so far i have my programing going to the website i want to download link from and selecting it, then the firefox dialogue box shows up and i don't know what to do. i want to save this file to a folder on my desktop. I am using this for a nightly build so i need this to work. Please help.
好的,到目前为止,我已经将我的程序转到了我想从中下载链接并选择它的网站,然后出现了 Firefox 对话框,但我不知道该怎么做。我想将此文件保存到我桌面上的一个文件夹中。我正在使用它进行夜间构建,所以我需要它来工作。请帮忙。
Here is my code that grabs the download link from the website:
这是我从网站上获取下载链接的代码:
driver = web driver.Firefox()
driver.implicitly_wait(5)
driver.get("Name of web site I'm grabbing from")
driver.find_element_by_xpath("//a[contains(text(), 'DEV.tgz')]".click()
采纳答案by alecxe
You need to make Firefoxsave this particular file type automatically.
您需要Firefox自动保存此特定文件类型。
This can be achieved by setting browser.helperApps.neverAsk.saveToDiskpreference:
这可以通过设置browser.helperApps.neverAsk.saveToDisk首选项来实现:
from selenium import webdriver
profile = webdriver.FirefoxProfile()
profile.set_preference("browser.download.folderList", 2)
profile.set_preference("browser.download.manager.showWhenStarting", False)
profile.set_preference("browser.download.dir", 'PATH TO DESKTOP')
profile.set_preference("browser.helperApps.neverAsk.saveToDisk", "application/x-gzip")
driver = webdriver.Firefox(firefox_profile=profile)
driver.get("Name of web site I'm grabbing from")
driver.find_element_by_xpath("//a[contains(text(), 'DEV.tgz')]").click()
More explanation:
更多解释:
browser.download.folderListtells it not to use defaultDownloadsdirectorybrowser.download.manager.showWhenStartingturns of showing download progressbrowser.download.dirsets the directory for downloadsbrowser.helperApps.neverAsk.saveToDisktells Firefox to automatically download the files of the selectedmime-types
browser.download.folderList告诉它不要使用默认Downloads目录browser.download.manager.showWhenStarting轮流显示下载进度browser.download.dir设置下载目录browser.helperApps.neverAsk.saveToDisk告诉 Firefox 自动下载选定的文件mime-types
You can view all these preferences at about:configin the browser. There is also a very detailed documentation page available here: About:config entries.
您可以about:config在浏览器中查看所有这些首选项。这里还有一个非常详细的文档页面:关于:配置条目。
Besides, instead of using xpathapproach, I would use find_element_by_partial_link_text():
此外,xpath我不会使用方法,而是使用find_element_by_partial_link_text():
driver.find_element_by_partial_link_text("DEV.tgz").click()
Also see:
另见:
回答by Ravinath Edirisinghe
If the application is generated dynamically (mime-types) using Chrome browser will be a better approach since the Chrome will not open the file download pop-up.But multiple download option should be enabled if you need multiple downloads.
如果应用程序是使用 Chrome 浏览器动态生成的(mime 类型)将是更好的方法,因为 Chrome 不会打开文件下载弹出窗口。但如果您需要多次下载,则应启用多个下载选项。

