硒未在FirefoxProfile中设置downloaddir [英] selenium doesn't set downloaddir in FirefoxProfile
本文介绍了硒未在FirefoxProfile中设置downloaddir的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想自动下载文件并将其保存在目录中,一切都已完成,但是Firefox仍将文件保存在用户下载文件夹中,例如C:\users\root\Downloads
i want to auto download files and save them in directory, everything is done but firefox stills save files in User download folder e.g. C:\users\root\Downloads
PyWebBot类中的函数
the function in class PyWebBot
@staticmethod
def FirefoxProfile(path, handlers):
from selenium import webdriver
profile = webdriver.FirefoxProfile()
profile.set_preference("browser.download.folderList",1)
profile.set_preference("browser.download.manager.showWhenStarting",False)
profile.set_preference("browser.download.dir", path)
profile.set_preference("browser.download.downloadDir", path)
profile.set_preference("browser.download.defaultFolder", path)
profile.set_preference("browser.helperApps.alwaysAsk.force", False)
profile.set_preference("browser.helperApps.neverAsk.saveToDisk", handlers)
profile.set_preference("pdfjs.disabled", True)
profile.update_preferences()
return profile
然后
def setUp(self):
self.profile = PyWebBot.FirefoxProfile(config['downloads'], config['handlers'])
self.driver = webdriver.Firefox(self.profile)
...
...
config:
config['downloads'] = 'Q:/web2py_src/web2py/applications/internet2letter/private/testing/selenium/downloads'
config['handlers'] = 'application/pdf'
推荐答案
有两种方法可以解决此问题,
There are couple methods to a solution for this problem,
- 确保路径有效.使用类似
os.path.exists
或os.isfile
的名称
- 使用硒驱动程序启动
Firefox
时,导航至about:config
并检查browser.download.dir
,以确保进行了更改. - 最后,确保
profile.set_preference
(profile.set_preference("browser.download.folderList",2
)具有2
作为第二个参数,因为0
表示要下载到桌面,1
表示要下载到默认的下载"目录,2
表示使用您在"browser.download.dir"中指定的目录 - 确保您的路径标有反斜杠"\"而不是正斜杠 斜杠"/"
- Make sure that the path is valid. Use something like,
os.path.exists
oros.isfile
- When the
Firefox
launches with the selenium driver, navigate toabout:config
and check the look upbrowser.download.dir
, to make sure there was a change. - Finally, make sure that
profile.set_preference
(profile.set_preference("browser.download.folderList",2
) has2
as a second argument, since0
means to download to the desktop,1
means to download to the default "Downloads" directory,2
means to use the directory you specify in "browser.download.dir" - Make sure your path is noted with back slashes '\' not forward slashes '/'
这篇关于硒未在FirefoxProfile中设置downloaddir的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文