2015-04-23 104 views
1

我想從sharepoint url下載文件並將代碼寫入neverask.savetodisk,但它仍然顯示保存文件的對話框。我嘗試了相同的代碼,它在我們點擊從其他URL的下載鏈接但不能與SharePoint應用程序一起工作時起作用。這裏是我使用的代碼...使用selenium webdriver從sharepoint下載文件python

from selenium import webdriver 
from selenium.webdriver.firefox.firefox_profile import FirefoxProfile 

# To prevent download dialog 
profile = webdriver.FirefoxProfile() 
profile.set_preference('browser.download.folderList', 2) # custom location 
profile.set_preference('browser.download.manager.showWhenStarting', False) 
profile.set_preference("browser.download.defaultFolder",'tt_at'); 
profile.set_preference("browser.download.lastDir",'tt_at'); 
profile.set_preference('browser.download.dir', 'tt_at') 
profile.set_preference("browser.download.useDownloadDir",True); 
profile.set_preference('browser.helperApps.neverAsk.saveToDisk', "application/octet-stream,application/msexcel") 

browser = webdriver.Firefox(profile) 
browser.get("https://docs.ad.sys.com/sites/cloud/Project/Form/FolderCTID=0x01200069047C40C93C3846B74E0776AAD1610A&InitialTabId=Ribbon%2EDocument&VisibilityContext=WSSTabPersistence") 
browser.find_element_by_xpath('/html/body/form/div[8]/div/div[3]/div[3]/div[2]/div/div/table/tbody/tr/td/table/tbody/tr/td/div/table[1]/tbody/tr/td/table/tbody/tr[12]/td[4]/div[1]/a').click() 

但這個上面的代碼仍然顯示對話框來選擇位置。

+0

你能嘗試添加該語句'profile.set_preference( 「browser.helperApps.alwaysAsk.force」,假);'?也看到這個問題 - http://stackoverflow.com/questions/23466154/how-to-handle-file-download-popup-using-selenium-webdriver – LittlePanda

回答

0

我想我得到了解決,請嘗試以下操作:

from selenium import webdriver 
from selenium.webdriver.firefox.firefox_profile import FirefoxProfile 
profile = webdriver.FirefoxProfile() 
#Give Complete Path of download dir 
profile.set_preference("browser.download.lastDir",r'd:\temp') 
profile.set_preference("browser.download.useDownloadDir",True) 
profile.set_preference("browser.download.manager.showWhenStarting",False) 
profile.set_preference('browser.helperApps.neverAsk.saveToDisk',"application/vnd.ms-excel,Content-Type=application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,application/octet-stream") 
profile.set_preference('browser.helperApps.neverAsk.openFile', "application/vnd.ms-excel,Content-Type=application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,application/octet-stream") 
browser = webdriver.Firefox(profile) 
browser.get("https://docs.ad.sys.com/sites/cloud/Project/Form/FolderCTID=0x01200069047C40C93C3846B74E0776AAD1610A&InitialTabId=Ribbon%2EDocument&VisibilityContext=WSSTabPersistence") 
browser.find_element_by_xpath('/html/body/form/div[8]/div/div[3]/div[3]/div[2]/div/div/table/tbody/tr/td/table/tbody/tr/td/div/table[1]/tbody/tr/td/table/tbody/tr[12]/td[4]/div[1]/a').click() 

如果這還不能爲你工作,做到以下幾點,安裝插件tamperdata在Firefox,並觀察該文件的內容類型,你正試圖下載,然後將確切的文本添加到「browser.helperApps.neverAsk。*」首選項。這將解決你的問題!

enter image description here

相關問題