我想知道一旦启动驱动程序,如何在selenium中更新/更改下载位置?
在创建配置文件和启动webdriver期间设置下载目录没有问题。启动webdriver以根据数据类型更改目录后会出现此问题。
例如 -if dl doc是Docs \ Word中的单词保存 -if dl doc是pdf保存在Docs \ pdf
中这是我的代码
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()
答案 0 :(得分:5)
使用Firefox,一旦上下文设置为chrome
,就可以通过脚本注入在运行时更改首选项:
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"})