我想自动下载文件并将它们保存在目录中,一切都完成了,但 Firefox 仍然将文件保存在用户下载文件夹中,例如C:\users\root\Downloads
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['downloads'] = 'Q:/web2py_src/web2py/applications/internet2letter/private/testing/selenium/downloads'
config['handlers'] = 'application/pdf'