2012-11-29 67 views
3

我正在使用這段代碼來自動化一個流程,它會在沒有提示的情況下下載一個Winzip文件。 但它似乎沒有工作避免在Python/selenium自動化中下載文件對話框

profile = webdriver.firefox.firefox_profile.FirefoxProfile() 
profile.set_preference('browser.helperApps.neverAsk.saveToDisk',("application/zip, 
                application/octet-stream")) 
profile.set_preference('browser.download.folderList', 2) 
profile.set_preference('browser.download.dir', '/home/jack/DOWNLOAD') 
self.driver = webdriver.Firefox(firefox_profile=profile) 

我仍然看到被打開的對話框,而自動化。

+0

如果您在第一次下載時手動禁用了提示,那麼它在下載後是否能夠正常工作? – root

+0

@root不,我也無法手動完成。另外,如果你在linux上''about:config' – cppcoder

+0

'browser.download.dir'不存在,那麼下面的命令給你'curl -I https://www.downloadadress.com/ | grep Content-Type' – root

回答

3

我通過提取下載鏈接的href並使用python urllib模塊解決了這種情況。

使用下面的代碼,可以下載該文件並將其另存爲不同的文件名。

import urllib 
url = driver.find_element_by_link_text("Download").get_attribute("href"); 
urllib.urlretrieve(url, "saveasfilename") 
+0

有趣的是,我必須記住這一點:) – RocketDonkey

+0

@cppcoder我們可以提供目標文件夾嗎? – user2728397

+0

@cppcoder'urllib.request.urlretrieve' in python 3 – user2728397