Selenium Firefox Python:如何更改下载目录

Fai*_*oya 2 python firefox selenium python-behave

您好:/我目前在项目中设置下载文件夹时遇到问题。

我正在使用 Mac、Pycharm CE、Selenium、Python、Behave 和 Firefox(项目要求)。

我尝试了此配置Python Selenium Firefox 下载目录出现问题,但我不确定我做错了哪一部分。:/ 下载的文件仍位于默认的下载文件夹中。

from selenium import webdriver
from selenium.webdriver.firefox.options import Options

@given("First step")
def step_impl(context):
    download_path = "/Users/this/is/the/absolute/path/downloads"
    options = Options()
    options.set_preference("browser.download.folderList", '2')
    options.set_preference("browser.download.manager.showWhenStarting", False)
    options.set_preference("browser.download.dir", download_path)
    options.set_preference("browser.helperApps.neverAsk.saveToDisk", "text/csv")
    context.driver = webdriver.Firefox(options=options)
Run Code Online (Sandbox Code Playgroud)

小智 5

如何使用selenium / firefox / python切换下载目录?使用 Selenium 和 python将文件下载到指定位置

我建议尝试改变

options.set_preference("browser.download.dir", download_path)
Run Code Online (Sandbox Code Playgroud)
options.set_preference("browser.download.dir", '/Users/this/is/the/absolute/path/downloads')
Run Code Online (Sandbox Code Playgroud)

如果路径中有空格,请尝试

options.set_preference("browser.download.dir", r'/Users/this/is/the\ space/absolute/path/downloads')
Run Code Online (Sandbox Code Playgroud)