2015-04-23 45 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

相关问题