python - 这个 selenium firefox 配置文件将文件下载到自定义文件夹有什么问题?

标签 python python-3.x selenium firefox

我正在使用 selenium 和 python v3.6 使 firefox 自动将文件下载到自定义文件夹中。文件夹的位置是 C:/Users/username/Dropbox/Inv/

下面是我的 firefox 配置文件。

profile = webdriver.FirefoxProfile()
profile.set_preference('browser.download.folderList', 2) # custom location
profile.set_preference('browser.download.manager.showWhenStarting', False)
profile.set_preference('browser.download.dir', 'C:/Users/username/Dropbox/Inv/')
profile.set_preference('browser.helperApps.neverAsk.saveToDisk', 'text/plain')
profile.set_preference('browser.helperApps.neverAsk.openFile', 'text/plain')

目前,文件始终下载到默认文件夹 C:\Users\username\Downloads。如何让下载的文件夹位置为 C:/Users/username/Dropbox/Inv/

最佳答案

启动 Firefox 时需要使用 profile:

driver = webdriver.Firefox(firefox_profile = profile)

检查 8.4. How to auto save files using custom Firefox profile ?在 Selenium Docs FAQ 中。

这是链接中的示例:

import os

from selenium import webdriver

fp = webdriver.FirefoxProfile()

fp.set_preference("browser.download.folderList",2)
fp.set_preference("browser.download.manager.showWhenStarting",False)
fp.set_preference("browser.download.dir", os.getcwd())
fp.set_preference("browser.helperApps.neverAsk.saveToDisk", "application/octet-stream")

browser = webdriver.Firefox(firefox_profile=fp)
browser.get("http://pypi.python.org/pypi/selenium")
browser.find_element_by_partial_link_text("selenium-2").click()

关于python - 这个 selenium firefox 配置文件将文件下载到自定义文件夹有什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46803702/

相关文章:

python - 使用python在html源代码中查找图像

java - 如何处理 Selenium 中重复使用的元素(重复)

java - Selenium 元素并不总是可点击

python - 有没有比 Python 的 strftime 更快的替代方法?

python - Django 序列化程序嵌套创建 : How to avoid N+1 queries on relations

python - 执行 python manage.py runserver 时 cmp 错误超出最大递归深度

python - 如何使用 Selenium for Firefox (geckodriver) 将 navigator.webdriver 设置为未定义

python - 如何在python中将指数值转换为字符串格式?

python-3.x - 使用 Keras 和 TensorFlow 后端可重现结果

python - xls 到 JSON 使用 python3 xlrd