使用selenium webdriver python从sharepoint下载文件 [英] Download file from sharepoint using selenium webdriver python

查看:400
本文介绍了使用selenium webdriver python从sharepoint下载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从sharepoint url下载文件并将代码写入 neverask.savetodisk ,但仍显示保存文件的对话框。我尝试了相同的代码,当我们从其他URL点击下载链接但不能与sharepoint应用程序一起工作。这里是我使用的代码...

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

#防止下载对话框
profile = webdriver.FirefoxProfile()
profile.set_preference('browser.download.folderList',2)#自定义位置
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 /体/形式/ DIV [8] / DIV / DIV [3] / DIV [3] / DIV [2] / DIV / DIV /表/ tbody的/ TR / TD /表/ tbody的/ TR / TD / DIV / table [1] / tbody / tr / td / table / tbody / tr [12] / td [4] / div [1] / a')click()
pre>

但是上面的代码仍然显示选择位置的对话框。

解决方案

p>我想我得到了解决方案,尝试以下:

 从selenium import webdriver 
从selenium.webdriver。 firefox.firefox_profile import FirefoxProfile
profile = webdriver.FirefoxProfile()
#Give完整路径下载dir
profile.set_pref erence(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,应用程序/八位字节流)
browser = webdriver.Firefox(配置文件)
browser.get(https://docs.ad.sys.com/sites/cloud/Project/Form/FolderCTID= [/ b] / div / div [3] / div [3] / div [3] / div [3] / div [3] / div [3] / div [3] / DIV / DIV /表/ tbody的/ TR / TD /表/ tbody的/ TR / TD / DIV /表[1] / tbody的/ tr / td / table / tbody / tr [12] / td [4] / div [1] / a')click()

如果这还不适用于您,请执行以下操作,安装附件 tamperdata ,并观察您尝试下载的文件的内容类型,然后将该确切文本添加到browser.helperApps.neverAsk。*首选项。这将解决您的问题!




I am trying to download file from sharepoint url and written code to neverask.savetodisk but still it is showing dialog to save file. I tried same code and it works when we click download link from other URL but not working with sharepoint application. Here is code what i used...

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

but this above code still showing dialog to select location.

解决方案

I think I got the solution, try the following:

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

If this is also not working for you, do the following, install the addon tamperdata in firefox and observe the content type for the file you are trying to download and then add that exact text to "browser.helperApps.neverAsk.*" preference. That shall solve your problem!

这篇关于使用selenium webdriver python从sharepoint下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆