从 selenium 调用 firefox 时无法覆盖 "save" 选项-python

Unable to over-ride "save" options while invoking firefox from selenium-python

下面是代码,我用来调用 firefox 从网站下载 xlsx 文件。

from selenium import webdriver

options=webdriver.FirefoxOptions()
dest_dir = "myDir"

print (options.preferences)
#options.set_preference('profile', profile_path)
#options.set_preference("browser.download.useDownloadDir",True)
options.set_preference("browser.download.folderList",2)
options.set_preference("browser.download.dir",dest_dir)
options.set_preference("browser.download.manager.showWhenStarting",False)
options.set_preference("browser.helperApps.neverAsk.saveToDisk","application/xlsx/xls")
print (options.preferences)

driver = webdriver.Firefox(options=options)

即使有这些选项,每次我调用 firefox 时,它都会提出以下问题。

编辑: 更改后 options.set_preference("browser.helperApps.neverAsk.saveToDisk","application/xlsx/xls")options.set_preference("browser.helperApps.neverAsk.saveToDisk","application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet") 问题已解决。因此,我为 xlsx 文件提供了错误的 MIME 类型。参考 What is a correct MIME type for .docx, .pptx, etc.? 以获得正确的类型。

非常感谢@cruisepandey 和@djmonki 的帮助

我通常会在我的项目中调用这些首选项,它们可以无缝地工作。

代码:

options.set_preference("browser.download.panel.shown", False)
options.set_preference("browser.helperApps.neverAsk.openFile","text/csv,application/vnd.ms-excel")
options.set_preference("browser.helperApps.neverAsk.saveToDisk", "application/msword, application/csv, application/ris, text/csv, image/png, application/pdf, text/html, text/plain, application/zip, application/x-zip, application/x-zip-compressed, application/download, application/octet-stream");
options.set_preference("browser.download.manager.showWhenStarting", False);
options.set_preference("browser.download.manager.alertOnEXEOpen", False);
options.set_preference("browser.download.manager.focusWhenStarting", False);
options.set_preference("browser.download.folderList", 2);
options.set_preference("browser.download.useDownloadDir", True);
options.set_preference("browser.helperApps.alwaysAsk.force", False);
options.set_preference("browser.download.manager.alertOnEXEOpen", False);
options.set_preference("browser.download.manager.closeWhenDone", True);
options.set_preference("browser.download.manager.showAlertOnComplete", False);
options.set_preference("browser.download.manager.useWindow", False);
options.set_preference("services.sync.prefs.sync.browser.download.manager.showWhenStarting", False);
options.set_preference("pdfjs.disabled", True);
options.set_preference("browser.download.dir", "C:\Users\userid\Desktop\Automation")

改变后

options.set_preference("browser.helperApps.neverAsk.saveToDisk","application/xlsx/xls")

options.set_preference("browser.helperApps.neverAsk.saveToDisk","application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")

问题已解决。因此,我为 xlsx 文件提供了错误的 MIME 类型。参考 What is a correct MIME type for .docx, .pptx, etc.? 以获得正确的类型。

非常感谢@cruisepandey 和@djmonki 的帮助