简体   繁体   English

创建Webdriver后Selenium Firefox配置文件更新下载目录

[英]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.

相关问题 Selenium firefox 配置文件下载目录首选项未实现 - Selenium firefox profile download directory preference not getting implemented 下载一些CSV文件后,Selenium Webdriver和Firefox崩溃 - Selenium Webdriver and Firefox crashes after download a few CSV files Selenium Webdriver完全下载10个文件后冻结Firefox - Selenium Webdriver freeze Firefox after download exactly 10 files 使用python硒创建Firefox配置文件后如何将其保存并重用到磁盘上? - How to save and reuse a Firefox profile to disk after creating it with python selenium? 在python中使用默认的firefox配置文件和selenium webdriver - Using the default firefox profile with selenium webdriver in python Selenium Firefox Webdriver不采用配置文件 - Selenium Firefox webdriver does not adopt profile Firefox + Selenium WebDriver并自动下载csv文件 - Firefox + Selenium WebDriver and download a csv file automatically 无法使用 selenium webdriver + firefox 下载 PDF - Can't download PDF with selenium webdriver + firefox 使用 Selenium 和 Python 定义 webdriver 后如何更改 firefox 配置文件首选项 - How to change firefox profile preference after defining webdriver using Selenium and Python Python 中的 Selenium 下载文件:即使在设置 Firefox Profile 后,下载窗口也会打开 - Selenium in Python to download file: even after setting Firefox Profile the Download Window opens
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM