在无头模式下通过 Google Chrome 下载文件 [英] Download file through Google Chrome in headless mode

查看:39
本文介绍了在无头模式下通过 Google Chrome 下载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在以正常"模式在 Cromedrive 中编写代码并且工作正常.当我更改为无头模式时,它不会下载文件.我已经尝试了我在互联网上找到的代码,但没有用.

chrome_options = Options()chrome_options.add_argument("--headless")self.driver = webdriver.Chrome(chrome_options=chrome_options, executable_path=r'{}/chromedriver'.format(os.getcwd()))self.driver.set_window_size(1024, 768)self.driver.command_executor._commands["send_command"] = ("POST", '/session/$sessionId/chromium/send_command')params = {'cmd': 'Page.setDownloadBehavior', 'params': {'behavior': 'allow', 'downloadPath': os.getcwd()}}self.driver.execute("send_command", params)

有人知道如何解决这个问题吗?

PS:我不一定需要使用 Chomedrive.如果它在另一个驱动器中工作,那对我来说没问题.

解决方案

先解决

<块引用>

最低先决条件:

  • Selenium 客户端版本:

    <小时>

    详情

    通过 Headless Chromium 下载文件是其中之一自 Headless Chrome 推出以来最受欢迎的功能.

    从那以后,不同的贡献者发布了不同的解决方法,其中一些是:

    现在,好消息是Chromium团队已经正式宣布了通过Headless Chromium下载文件的功能.

    <小时>

    在讨论中 Headless 模式不保存文件下载 @eseckler 提到:

    <块引用>

    无头工作中的下载略有不同.有 Page.setDownloadBehavior devtools 命令来设置下载文件夹.我们正在研究一种使用 DevTools 网络拦截的方法,以通过 DevTools 流式传输下载的文件.

    可以在 问题 696481:无头模式下找到详细讨论t 保存文件下载

    最后,@bugdroid 修订版似乎为我们解决了这个问题.

    <小时>

    [ChromeDriver] 支持无头模式下载文件

    <块引用>

    以前,在无头模式下运行的 Chromedriver 无法正确下载文件,因为它会稀疏地解析提供给它的首选项文件.Headless chrome 团队的工程师建议使用 DevTools 的Page.setDownloadBehavior"来解决这个问题.此更改列表实现了此修复.下载的文件默认为当前目录,可以在实例化 chromedriver 实例时使用 download_dir 进行设置.还添加了测试以确保正确的下载功能.

    这是修订版提交

    来自 ChromeDriver v77.0.3865.40 (2019-08-20) 发行说明:

    已解决问题 2454:无头模式不保存文件下载 [Pri-2]

    <小时>

    解决方案

    <小时>

    尾声

    然而,Mac OSX 用户需要等待他们的馅饼 在 Chromedriver 上,在 MacOSX 上发送 Page.setDownloadBehavior 后无头 chrome 崩溃.

    I'm do me code in Cromedrive in 'normal' mode and works fine. When I change to headless mode it don't download the file. I already try the code I found alround internet, but didn't work.

    chrome_options = Options()
    chrome_options.add_argument("--headless")
    self.driver = webdriver.Chrome(chrome_options=chrome_options, executable_path=r'{}/chromedriver'.format(os.getcwd()))
    self.driver.set_window_size(1024, 768)
    self.driver.command_executor._commands["send_command"] = ("POST", '/session/$sessionId/chromium/send_command')
    
    params = {'cmd': 'Page.setDownloadBehavior', 'params': {'behavior': 'allow', 'downloadPath': os.getcwd()}}
    self.driver.execute("send_command", params)
    

    Anyone have any idea about how solve this problem?

    PS: I don't need to use Chomedrive necessarily. If it works in another drive it's fine for me.

    解决方案

    First the solution

    Minimum Prerequisites:

    To download the file clicking on the element with text as Download Data within this website you can use the following solution:

    • Code Block:

      from selenium import webdriver
      from selenium.webdriver.common.by import By
      from selenium.webdriver.support.ui import WebDriverWait
      from selenium.webdriver.support import expected_conditions as EC
      from selenium.webdriver.chrome.options import Options
      
      options = Options()
      options.add_argument("--headless")
      options.add_argument("--window-size=1920,1080")
      options.add_experimental_option("excludeSwitches", ["enable-automation"])
      options.add_experimental_option('useAutomationExtension', False)
      driver = webdriver.Chrome(chrome_options=options, executable_path=r'C:UtilityBrowserDriverschromedriver.exe', service_args=["--log-path=./Logs/DubiousDan.log"])
      print ("Headless Chrome Initialized")
      params = {'behavior': 'allow', 'downloadPath': r'C:UsersDebanjan.BDownloads'}
      driver.execute_cdp_cmd('Page.setDownloadBehavior', params)
      driver.get("https://www.mockaroo.com/")
      driver.execute_script("scroll(0, 250)"); 
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button#download"))).click()
      print ("Download button clicked")
      #driver.quit()
      

    • Console Output:

      Headless Chrome Initialized
      Download button clicked
      

    • File Downloading snapshot:


    Details

    Downloading files through Headless Chromium was one of the most sought functionality since Headless Chrome was introduced.

    Since then there were different work-arounds published by different contributors and some of them are:

    Now the, the good news is Chromium team have officially announced the arrival of the functionality Downloading file through Headless Chromium.


    In the discussion Headless mode doesn't save file downloads @eseckler mentioned:

    Downloads in headless work a little differently. There's the Page.setDownloadBehavior devtools command to set a download folder. We're working on a way to use DevTools network interception to stream the downloaded file via DevTools as well.

    A detailed discussion can be found at Issue 696481: Headless mode doesn't save file downloads

    Finally, @bugdroid revision seems to have nailed the issue for us.


    [ChromeDriver] Added support for headless mode to download files

    Previously, Chromedriver running in headless mode would not properly download files due to the fact it sparsely parses the preference file given to it. Engineers from the headless chrome team recommended using DevTools's "Page.setDownloadBehavior" to fix this. This changelist implements this fix. Downloaded files default to the current directory and can be set using download_dir when instantiating a chromedriver instance. Also added tests to ensure proper download functionality.

    Here is the revision and commit

    From ChromeDriver v77.0.3865.40 (2019-08-20) release notes:

    Resolved issue 2454: Headless mode doesn't save file downloads [Pri-2]
    


    Solution


    Outro

    However Mac OSX users have a wait for their pie as On Chromedriver, headless chrome crashes after sending Page.setDownloadBehavior on MacOSX.

    这篇关于在无头模式下通过 Google Chrome 下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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