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

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

问题描述

我正在尝试从 sharepoint url 下载文件并将代码写入 neverask.savetodisk 但它仍然显示保存文件的对话框.我尝试了相同的代码,当我们单击来自其他 URL 的下载链接但不适用于 sharepoint 应用程序时,它可以工作.这是我使用的代码...

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:	emp')
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 并观察您尝试下载的文件的内容类型,然后将该确切文本添加到browser.helperApps.neverAsk.*"首选项.这将解决您的问题!

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 从共享点下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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