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