[英]Selenium firefox profile update download directory after creating webdriver
I'm wondering how can I update/change download location in selenium once I started driver? 我想知道启动驱动程序后如何更新/更改硒中的下载位置?
it is not problem to set download dir during creation of profile and initiation of webdriver. 创建配置文件和启动Webdriver期间设置下载目录不是问题。 The problem appears after initiation of webdriver to change directory depending on data type. 启动webdriver根据数据类型更改目录后出现问题。
For example -if dl doc is word save in Docs\\Word -if dl doc is pdf save in Docs\\pdf 例如,-如果dl doc是Word保存在Docs \\ Word中-如果dl doc是pdf保存在Docs \\ pdf中
this is my code 这是我的代码
profile = webdriver.FirefoxProfile()
profile.set_preference('browser.download.dir', os.getcwd())
profile.set_preference("browser.download.folderList", 2)
profile.set_preference('browser.helperApps.neverAsk.saveToDisk', 'application/download,application/octet-stream,application/pdf')
profile.update_preferences()
driver = webdriver.Firefox(firefox_profile=profile)
driver.delete_all_cookies()
sleep(10)
# this part doesn't work
driver.profile.set_preference('browser.download.dir',"{0}\{1}".format(os.getcwd(),"Docs"))
driver.profile.update_preferences()
With Firefox it's possible to change the preferences at run-time with a scrip injection once the context is set to chrome
: 有了Firefox,一旦上下文设置为chrome
就可以在运行时通过scrip注入更改首选项:
def set_download_dir(driver, directory):
driver.command_executor._commands["SET_CONTEXT"] = ("POST", "/session/$sessionId/moz/context")
driver.execute("SET_CONTEXT", {"context": "chrome"})
driver.execute_script("""
Services.prefs.setBoolPref('browser.download.useDownloadDir', true);
Services.prefs.setStringPref('browser.download.dir', arguments[0]);
""", directory)
driver.execute("SET_CONTEXT", {"context": "content"})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.