如何使用Robot Framework + Selenium Library强制下载PDF附件?

时间:2020-06-26 14:45:24

标签: selenium-webdriver robotframework content-disposition firefox-profile

在我的测试自动化套件中,我需要强制下载PDF文件。所讨论的文件是具有MIME类型的应用程序/八位字节流的PDF,此外,服务器会分配

Content-Disposition: attachment;
X-Download-Options: noopen 

到文件。

单击链接后,将出现提示:

enter image description here

我知道初始化Open Browser关键字时可以设置的选项,所以我做了以下事情:

*** Settings ***
Library    SeleniumLibrary

*** Variables ***
${PREFERENCES}    set_preference("browser.download.dir",r"C:${/}DL");set_preference("browser.download.lastDir",r"C:${/}DL");set_preference("browser.download.folderList", 2);set_preference("browser.helperApps.alwaysAsk.force", False);set_preference("browser.helperApps.neverAsk.openFile", "application/octet-stream, application/pdf")


*** Test Cases ***
Initial Setup

*** Keywords ***
Initial Setup
    Open Browser    about:blank    Firefox    ff_profile_dir=${PREFERENCES}

但是,仅设置这样的首选项是不够的,提示仍然存在。但是,当我调试自动化套件时,在该浏览器实例中,将PDF文件的选项手动更改为“保存文件”: enter image description here

有效。设置该选项后,将更新名为handlers.json的文件:

{"defaultHandlersVersion":{"en-US":4},"mimeTypes":{"application/pdf":{"action":0,"extensions":["pdf"]}},"schemes":{"irc":{"stubEntry":true,"handlers":[null,{"name":"Mibbit","uriTemplate":"https://www.mibbit.com/?url=%s"}]},"ircs":{"stubEntry":true,"handlers":[null,{"name":"Mibbit","uriTemplate":"https://www.mibbit.com/?url=%s"}]},"mailto":{"stubEntry":true,"handlers":[null,{"name":"Yahoo! Mail","uriTemplate":"https://compose.mail.yahoo.com/?To=%s"},{"name":"Gmail","uriTemplate":"https://mail.google.com/mail/?extsrc=mailto&url=%s"}]}}}

使用Selenium初始化Firefox时如何设置值?

我也尝试手动创建自定义Firefox配置文件,然后定义Selenium库以在Open Browser中读取该自定义配置文件,但这不是可行的选择,因为我不想将该自定义配置文件存储在版本控制系统中。

1 个答案:

答案 0 :(得分:0)

只需添加到 ${PREFERENCES} : set_preference("pdfjs.disabled", True)

相关问题