拨开荷叶行,寻梦已然成。仙女莲花里,翩翩白鹭情。
IMG-LOGO
主页 文章列表 Selenium设定首选项下载excel档案但弹出对话框?

Selenium设定首选项下载excel档案但弹出对话框?

白鹭 - 2022-02-11 2240 0 0

我正在尝试excel使用 selenium下载档案firefox

def download_driver(location_for_download):
    profile = webdriver.FirefoxProfile()
    profile.set_preference("browser.download.folderList", 2)
    profile.set_preference("browser.download.manager.showWhenStarting", False)
    profile.set_preference("browser.download.dir", location_for_download)
    profile.set_preference("browser.helperApps.neverAsk.saveToDisk",
                           "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
    profile.set_preference("browser.helperApps.neverAsk.saveToDisk", "application/csv")
    profile.set_preference("browser.helperApps.neverAsk.saveToDisk", "text/plain")
    profile.set_preference("browser.helperApps.neverAsk.saveToDisk", "text/comma-separated-values")
    profile.set_preference("browser.helperApps.neverAsk.saveToDisk", "application/xls")
    driver = webdriver.Firefox(firefox_profile=profile)
    return driver

当我在脚本中使用这个函式时,会弹出保存档案的对话框。我在ubuntu我认为这MIME-type是错误的,我已经添加了所有我能想到和搜索的内容。

有问题的档案显示为2007 excel spreadsheet.

uj5u.com热心网友回复:

找到正确的MIME type

将以下内容添加到函式中使其作业:

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

把这个问题搁置一旁,以防其他人来找。

uj5u.com热心网友回复:

有点不同的解决方案,但作业得非常可靠。

profile = FirefoxProfile()
profile.set_preference("browser.download.panel.shown", False)
profile.set_preference("browser.helperApps.neverAsk.openFile","text/csv,application/vnd.ms-excel")
profile.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");
profile.set_preference("browser.download.manager.showWhenStarting", False);
profile.set_preference("browser.download.manager.alertOnEXEOpen", False);
profile.set_preference("browser.download.manager.focusWhenStarting", False);
profile.set_preference("browser.download.folderList", 2);
profile.set_preference("browser.download.useDownloadDir", True);
profile.set_preference("browser.helperApps.alwaysAsk.force", False);
profile.set_preference("browser.download.manager.alertOnEXEOpen", False);
profile.set_preference("browser.download.manager.closeWhenDone", True);
profile.set_preference("browser.download.manager.showAlertOnComplete", False);
profile.set_preference("browser.download.manager.useWindow", False);
profile.set_preference("services.sync.prefs.sync.browser.download.manager.showWhenStarting", False);
profile.set_preference("pdfjs.disabled", True);
标签:

0 评论

发表评论

您的电子邮件地址不会被公开。 必填的字段已做标记 *