我通常使用以下函数为Firefox设置配置文件
def FirefoxProfile(path, handlers):
debug = True
if debug: print "%r - %s(%r, %r)" % (time.asctime(), "FirefoxProfile", path, handlers)
profile = webdriver.FirefoxProfile()
profile.set_preference("browser.download.manager.showWhenStarting",False)
if (not(isfile(path)) & exists(path)):
profile.set_preference("browser.download.folderList",2)
profile.set_preference("browser.download.dir", path)
profile.set_preference("browser.download.downloadDir", path)
profile.set_preference("browser.download.defaultFolder", path)
else:
profile.set_preference("browser.download.folderList",1)
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()
if debug: print "done. - %r" % time.asctime()
return profile
# Somewhere else
self.profile = PyWebBot.FirefoxProfile(config['downloads'], config['handlers'])
self.driver = webdriver.Firefox(self.profile)
如何为Chrome执行此操作?我需要相同的设置
def Google_desired_capabilities(path=os.getcwd()):
return {"prefs": {
"download.default_directory": path,
"download.prompt_for_download": False
},
"switches": ["-silent", "--disable-logging"],
"chromeOptions": {
"args": ["-silent", "--disable-logging"]
}
}
chrome_options = webdriver.ChromeOptions()
chrome_options.prefs = PyWebBot.Google_desired_capabilities(config['downloads'])['prefs']
self.driver = webdriver.Chrome(path.join(config['drivers'],'chromedriver.exe'), chrome_options=chrome_options)