Robotframework 如何在robot框架中启用使用无头chrome浏览器下载文件?

Robotframework 如何在robot框架中启用使用无头chrome浏览器下载文件?,robotframework,headless-browser,google-chrome-headless,Robotframework,Headless Browser,Google Chrome Headless,如何使用chrome开发工具中的Page.setDownloadBehavior进行传递,以便使用下面的代码设置headless chrome的下载行为 Create Chrome Browser [Arguments] ${link_to_open} ${chrome_options}= Evaluate sys.modules['selenium.webdriver'].ChromeOptions() sys, selenium.webdriv

如何使用chrome开发工具中的Page.setDownloadBehavior进行传递,以便使用下面的代码设置headless chrome的下载行为

    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下载文件时,似乎有“安全功能”。这将在本节中进一步讨论。这就指向了StackOverflow的答案。在这个答案中,给出了一个Python示例(),它可以轻松地转换为Robot框架库:

无头下载.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的答案非常适合此示例。添加headless模式的额外行、附加库和工作示例:

无头机器人下载

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

因此,简而言之:如果没有其他人对堆栈溢出的贡献,这个答案是不可能的。如果这为您提供了一个有效的解决方案,请按照其他答案的链接并对这些答案进行投票。

在无头模式下尝试使用Chrome下载文件时,似乎有一个“安全功能”。这将在本节中进一步讨论。这就指向了StackOverflow的答案。在这个答案中,给出了一个Python示例(),它可以轻松地转换为Robot框架库:

无头下载.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的答案非常适合此示例。添加headless模式的额外行、附加库和工作示例:

无头机器人下载

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

因此,简而言之:如果没有其他人对堆栈溢出的贡献,这个答案是不可能的。如果这为您提供了一个有效的解决方案,请按照其他答案的链接并对这些答案进行投票。

嗨,a.Kootstra,我尝试了python调用,但仍然没有成功。我在日志中看到了这一点:18:08:46.018 DEBUG POST{“sessionId”:“c5aa448ceadbb7c41fb1de629a2328c”,“params”:{“downloadPath”:“C:\\Temp\\Downloads”,“behavior:“allow”},“cmd:“Page.setdownloadbbehavior”}18:08:46.034调试完成请求结果:值:无结果:状态:0这是否正常?它仍然无法下载。有人能帮我吗?您正在运行哪个版本的Selenium(2)库、Selenium Python模块和Webdriver。这是按原样运行示例时出现的错误,还是您根据自己的需要对其进行了修改?只尝试了下面的示例,但我正在从公司的intranet/内部web应用程序上下载。嗨,A.Kootstra,我尝试了python调用,但仍然没有成功。我在日志中看到了这一点:18:08:46.018 DEBUG POST{“sessionId”:“c5aa448ceadbb7c41fb1de629a2328c”,“params”:{“downloadPath”:“C:\\Temp\\Downloads”,“behavior:“allow”},“cmd:“Page.setdownloadbbehavior”}18:08:46.034调试完成请求结果:值:无结果:状态:0这是否正常?它仍然无法下载。有人能帮我吗?您正在运行哪个版本的Selenium(2)库、Selenium Python模块和Webdriver。这是按原样运行示例时出现的错误,还是您根据自己的需要对其进行了修改?仅尝试了下面的示例,但我正在从公司的intranet/internal web应用程序上下载。