如何在机器人框架中使用无头 chrome 浏览器启用下载文件? [英] How can I enable download a file using a headless chrome browser in robot framework?

查看:200
本文介绍了如何在机器人框架中使用无头 chrome 浏览器启用下载文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 chrome 开发工具中使用 Page.setDownloadBehavior 进行传递,以便我可以使用以下代码设置无头 chrome 的下载行为?

How can i use Page.setDownloadBehavior in chrome dev tools to pass on so that I can set the download behavior for headless chrome with the code below?

    Create Chrome Browser
    [Arguments]    ${link_to_open}
    ${chrome_options}=    Evaluate    sys.modules['selenium.webdriver'].ChromeOptions()    sys, selenium.webdriver
    ${prefs}=    Create Dictionary    download.default_directory=${DOWNLOADS_DIR}
   Call Method    ${chrome options}    add_argument    headless 
   Call Method    ${chrome options}    add_argument    disable-gpu
  Selenium2Library.Go To    ${link_to_open}

推荐答案

尝试在无头模式下使用 Chrome 下载文件时,似乎存在安全功能".这在 Chromium 问题跟踪器 中有进一步讨论.这指向使用铬无头和硒下载的StackOverflow答案.在这个答案中,给出了一个 Python 示例(GitHub),它可以轻松转换为机器人框架库:

There appears to be a 'security feature' when trying to download a file using Chrome in headless mode. This is further discussed in the Chromium issue tracker. This pointed to the StackOverflow answer for Downloading with chrome headless and selenium. In this answer a Python example is given (GitHub) which converts easily to a Robot Framework Library:

headless_download.py

class headless_download(object):

    ROBOT_LIBRARY_VERSION = 1.0

    def __init__(self):
        pass

    def enable_download_in_headless_chrome(self, driver, download_dir):
        """
        there is currently a "feature" in chrome where
        headless does not allow file download: https://bugs.chromium.org/p/chromium/issues/detail?id=696481
        This method is a hacky work-around until the official chromedriver support for this.
        Requires chrome version 62.0.3196.0 or above.
        """

        # add missing support for chrome "send_command"  to selenium webdriver
        driver.command_executor._commands["send_command"] = ("POST", '/session/$sessionId/chromium/send_command')

        params = {'cmd': 'Page.setDownloadBehavior', 'params': {'behavior': 'allow', 'downloadPath': download_dir}}
        command_result = driver.execute("send_command", params)
        print("response from browser:")
        for key in command_result:
            print("result:" + key + ":" + str(command_result[key]))

下载文件时,StackOverlow 对 Robot Framework Download File 的回答非常适合这个例子.为无头模式添加额外的行、额外的库和一个工作示例:

When downloading a file, the StackOverlow answer for Robot Framework Download File that fits this example perfectly. Adding the extra lines for headless mode, additional library and a working example:

headless_download.robot

*** Settings ***
Test Teardown     Close All Browsers
Library           Selenium2Library
Library           OperatingSystem
Library           headless_download

*** Test Cases ***
Download PDF
  # 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

  # list of plugins to disable. disabling PDF Viewer is necessary so that PDFs are saved rather than displayed
  ${disabled}    Create List    Chrome PDF Viewer
  ${prefs}    Create Dictionary    download.default_directory=${download directory}    plugins.plugins_disabled=${disabled}
  Call Method    ${chrome options}    add_experimental_option    prefs    ${prefs}

  Call Method    ${chrome options}    add_argument    headless 
  Call Method    ${chrome options}    add_argument    disable-gpu

  Create Webdriver    Chrome    chrome_options=${chrome options}

  Go To    http://www.sample-videos.com/download-sample-text-file.php

  ${S2L}           get library instance    Selenium2Library
  ${webdriver}    Call Method             ${S2L}    _current_browser
  Enable Download In Headless Chrome    ${webdriver}    ${download directory} 

  Click Link    xpath=//a[@data='1']

  # wait for download to finish
  ${file}    Wait Until Keyword Succeeds    1 min    2 sec    Download should be done    ${download directory}

*** Keywords ***
Download should be done
  [Arguments]    ${directory}
  [Documentation]    Verifies that the directory has only one folder and it is not a temp file.
  ...
  ...    Returns path to the file
  ${files}    List Files In Directory    ${directory}
  Length Should Be    ${files}    1    Should be only one file in the download folder
  Should Not Match Regexp    ${files[0]}    (?i).*\\.tmp    Chrome is still downloading a file
  ${file}    Join Path    ${directory}    ${files[0]}
  Log    File was successfully downloaded to ${file}
  [Return]    ${file}

所以,简而言之:如果没有其他人在 Stack Overflow 上的贡献,这个答案是不可能的.如果这为您提供了可行的解决方案,请点击其他答案的链接,并为这些答案点赞.

So, in short: this answer was not possible without the contributions by others on Stack Overflow. If this provides you with a working solution, please follow the links to the other answers and upvote those answers as well.

这篇关于如何在机器人框架中使用无头 chrome 浏览器启用下载文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆