【问题标题】:How to download a pdf file automatically in chrome browser using Robot Framework如何使用机器人框架在 chrome 浏览器中自动下载 pdf 文件
【发布时间】:2017-10-24 16:50:48
【问题描述】:

作为我测试的一部分,我想在浏览器上加载 pdf 文件时自动从浏览器下载它。有人可以指导我如何实现这一点。以下是我尝试过的方式。我尝试单击下载按钮,但它对我不起作用。以下是浏览器上出现的截图。

*** Test Cases ***

    Download and deploy content package 

        ${output}=   Run keyword   job history
        Log to console   ${output}
        # create unique folder
        ${now}    Get Time    epoch
        ${download directory}    Join Path    ${OUTPUT DIR}    downloads_${now}
        Create Directory    ${download directory}
        ${chrome options}=    Evaluate    sys.modules['selenium.webdriver'].ChromeOptions()    sys, selenium.webdriver
        ${prefs}    Create Dictionary    prompt_for_download=false  download.default_directory=${download directory}    
        Call Method    ${chrome options}    add_experimental_option    prefs    ${prefs}
        Create Webdriver    Chrome    chrome_options=${chrome options}
        Goto   ${output}   
        Sleep       5

【问题讨论】:

    标签: pdf robotframework selenium2library


    【解决方案1】:

    我有一个非常简单的文件下载解决方法。它有以下规则:

    • 在套件设置中创建下载文件夹

      Set Global Variable    ${global_downloadDir}    ${CURDIR}\\Downloads\\${suite_orgName}
      ${chromeOptions}=    Evaluate    sys.modules['selenium.webdriver'].ChromeOptions()    sys, selenium.webdriver
      ${prefs} =    Create Dictionary    download.default_directory=${global_downloadDir}
      Call Method    ${chromeOptions}    add_experimental_option    prefs    ${prefs}
      Create Webdriver    Chrome    chrome_options=${chromeOptions}
      
    • 在测试拆卸时清除文件夹

      Run Keyword And Ignore Error    Empty Directory    ${global_downloadDir}
      

      有了以上功能,您就可以轻松操作下载的文件了。

    【讨论】:

    【解决方案2】:

    虽然上面的答案很好,但对我来说,下面的代码有效。

    Conference Summary
         ${output}=   Run keyword   SLO conformance summary
        # create unique folder in current directory
        ${current_date}=    Get Current Date 
        ${folder_name} =    Convert Date    ${current_date}    result_format=${SUITE NAME}.%d.%m.%Y_%H.%M
        ${download directory}    Join Path    ${OUTPUT DIR}    ${folder_name}
        Create Directory    ${download directory}
        ${options}  Evaluate  sys.modules['selenium.webdriver'].ChromeOptions()  sys, selenium.webdriver
        Call Method  ${options}  add_argument  --start-maximized
        ${preferences} =    Create Dictionary   prompt_for_download=false  download.default_directory=${download directory}    download.directory_update=True
        Call Method  ${options}  add_experimental_option  prefs  ${preferences}
         Create WebDriver  Chrome  chrome_options=${options} 
        Go To    ${output}     
        sleep   4
        Page Should Not Contain Element  //div[@class='errorIcon']
        Run Keyword     Get export position
    

    【讨论】:

      猜你喜欢
      • 2018-07-02
      • 1970-01-01
      • 2020-06-04
      • 2018-04-01
      • 2016-05-19
      • 2022-01-22
      • 2018-05-21
      • 2013-02-07
      • 2018-11-06
      相关资源
      最近更新 更多