【问题标题】:Windows popup interaction for downloading using selenium webdriver in python在 python 中使用 selenium webdriver 进行下载的 Windows 弹出交互
【发布时间】:2017-10-25 18:42:18
【问题描述】:

我正在编写一个程序来使用 python 中的 selenium webdriver 自动下载数据。当我点击弹出窗口后的“下载”按钮时

.

选择默认选项“打开方式”。我希望我的程序首先单击“保存文件”选项,然后单击“确定”。 我使用以下代码来设置 Firefox 配置文件

    profile = webdriver.FirefoxProfile()
    profile.set_preference('browser.download.folderList', 2)
    profile.set_preference('browser.download.manager.showWhenStarting', False)
    profile.set_preference('browser.download.dir', os.getcwd())
    profile.set_preference('browser.helperApps.neverAsk.saveToDisk', "application/xlsx")

但它不适用于我的情况。然后我尝试使用以下代码从主窗口切换到此窗口

    parent_h = driver.current_window_handle
    handles = driver.window_handles
    handles.remove(parent_h)
    driver.switch_to_window(handles.pop())    

但现在我不知道如何与这个窗口进行交互?

【问题讨论】:

    标签: python selenium webdriver firefox-profile


    【解决方案1】:

    您应该尝试为xlsx 扩展名使用正确的MIME 类型的首选项,即"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",但不是"application/xlsx"

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

    您可以查看 Microsoft Office 文件hereMIME 类型列表

    【讨论】:

    • 使用此 mime 后,还会出现相同的弹出窗口。你能告诉我另一个解决方案吗?
    【解决方案2】:

    经过这么多发现和研究,我得到了以下代码,这对这种情况会有所帮助。

        profile = webdriver.FirefoxProfile()
        profile.set_preference("browser.download.dir",os.getcwd());
        profile.set_preference("browser.download.folderList",2);
        profile.set_preference("browser.helperApps.neverAsk.saveToDisk", "application/csv,application/excel,application/vnd.msexcel,application/vnd.ms-excel,text/anytext,text/comma-separated-values,text/csv,application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,application/octet-stream");
        profile.set_preference("browser.download.manager.showWhenStarting",False);
        profile.set_preference("browser.helperApps.neverAsk.openFile","application/csv,application/excel,application/vnd.msexcel,application/vnd.ms-excel,text/anytext,text/comma-separated-values,text/csv,application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,application/octet-stream");
        profile.set_preference("browser.helperApps.alwaysAsk.force", False);
        profile.set_preference("browser.download.manager.useWindow", False);
        profile.set_preference("browser.download.manager.focusWhenStarting", False);
        profile.set_preference("browser.download.manager.alertOnEXEOpen", False);
        profile.set_preference("browser.download.manager.showAlertOnComplete", False);
        profile.set_preference("browser.download.manager.closeWhenDone", True);
        profile.set_preference("pdfjs.disabled", True);
    

    【讨论】:

    • 在尝试了各种 SO 解决方案后,我尝试了您提到的 pdfjs.disabled,并且成功了。但是对我来说它只能与browser.helperApps.neverAsk.saveToDisk一起使用,不需要其他设置。
    猜你喜欢
    • 2015-03-03
    • 1970-01-01
    • 2018-09-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多