How to download a file using the remote selenium webdriver?

前端 未结 5 996
清歌不尽
清歌不尽 2020-12-09 18:24

I am using a remote selenium webdriver to perform some tests. At some point, however, I need to download a file and check its contents.

I am using the

5条回答
  •  猫巷女王i
    2020-12-09 19:16

    @FlorentB's answer for Chrome works up until Chrome version 79. For newer versions, the function get_downloaded_files needed to be updated as the downloads.Manager is not accessible anymore. However, this updated version should work with previous versions as well.

    def get_downloaded_files(driver):
    
      if not driver.current_url.startswith("chrome://downloads"):
        driver.get("chrome://downloads/")
    
      return driver.execute_script( \
         "return  document.querySelector('downloads-manager')  "
         " .shadowRoot.querySelector('#downloadsList')         "
         " .items.filter(e => e.state === 'COMPLETE')          "
         " .map(e => e.filePath || e.file_path || e.fileUrl || e.file_url); ")
    

提交回复
热议问题