python下载文件到指定目录-使用Selenium和python将文件下载到指定的位置

1586010002-jmsa.png

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.

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()

解决方案

You need to make Firefox save this particular file type automatically.

This can be achieved by setting browser.helperApps.neverAsk.saveToDisk preference:

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.folderList tells it not to use default Downloads directory

browser.download.manager.showWhenStarting turns of showing download progress

browser.download.dir sets the directory for downloads

browser.helperApps.neverAsk.saveToDisk tells Firefox to automatically download the files of the selected mime-types

You can view all these preferences at about:config in the browser. There is also a very detailed documentation page available here: About:config entries.

Besides, instead of using xpath approach, I would use find_element_by_partial_link_text():

driver.find_element_by_partial_link_text("DEV.tgz").click()

Also see:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值