How to download a pdf file automatically in chrome

2019-09-16 19:44发布

As a part of my test , i want to download a pdf file from browser automatically as soon as it loads on browser. Can someone guide me how this can be achieved.Following is the way that i have tried.I have tried clicking the download button but it didn't work for me. Following is the screenshot that appears on browser .enter image description here

*** 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

2条回答
对你真心纯属浪费
2楼-- · 2019-09-16 19:49

Although above answer is good but for me following code worked.

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
查看更多
对你真心纯属浪费
3楼-- · 2019-09-16 19:55

I have quite simple workaround for file downloads. It has following rules:

  • Create download folder in suite setup

    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}
    
  • Clear the folder on test teardown

    Run Keyword And Ignore Error    Empty Directory    ${global_downloadDir}
    

    With above, you'll be easily able to manipulate downloaded files.

查看更多
登录 后发表回答