Selenium python 更改默认下载目录,无需打开窗口弹出窗口

问题描述 投票:0回答:1

我想使用 Selenium 更改默认下载目录。我还想避免打开 Windows 对话框来选择名称,因为我无法与 Selenium 交互。如果我保留默认目录,它会直接下载文件,但如果我尝试更改它,它会打开 Windows 对话框。你有什么建议吗?谢谢你。

python google-chrome selenium-webdriver firefox
1个回答
0
投票

对于 Firefox,您可以按照以下步骤配置自动下载到您选择的位置:

from selenium import webdriver

download_location = "C:\\temp"

options = webdriver.FirefoxOptions()
options.set_preference("browser.download.panel.shown", False)
options.set_preference("browser.download.dir", download_location)
options.set_preference("browser.download.folderList", 2)
options.set_preference("browser.download.animateNotifications", False)
options.set_preference("browser.helperApps.alwaysAsk.force", False)
options.set_preference("browser.download.manager.showWhenStarting", False)
options.set_preference(
    "browser.helperApps.neverAsk.saveToDisk",
    (
        "application/pdf, application/zip, application/octet-stream, "
        "text/csv, text/xml, application/xml, text/plain, "
        "text/octet-stream, application/x-gzip, application/x-tar "
        "application/"
        "vnd.openxmlformats-officedocument.spreadsheetml.sheet"
    ),
)

driver = webdriver.Firefox(options=options)

# ...

driver.quit()
© www.soinside.com 2019 - 2024. All rights reserved.