Chrome headless file download with Selenium in Python

心已入冬 提交于 2019-11-28 04:10:45

问题


In headless mode, Chrome defaults to disallowing file downloads.

However, recently they added an option to DevTools to enable this behavior:

https://chromedevtools.github.io/devtools-protocol/tot/Page/#method-setDownloadBehavior

Using Selenium with ChromeDriver in Python, how do I allow file downloads?


回答1:


For those who are still looking , That's how i did it :

def enable_download_in_headless_chrome( driver, download_dir):
    #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}}
    driver.execute("send_command", params)



回答2:


Here's the chromedriver ticket to add support for headless file downloads: https://bugs.chromium.org/p/chromedriver/issues/detail?id=1973

File downloading is disabled when using current version of headless Chrome (#60). Support to enable downloading is added to version # 62, which is currently in Dev channel. Need ChromeDriver to support this feature too.

It also references https://bugs.chromium.org/p/chromium/issues/detail?id=696481 which has reproduction steps for the issue:

Chrome Version : Chromium 58.0.3023.0

What steps will reproduce the problem? (1) Set headless mode (--headless) on command-line (2) Point URL to downloadable file (3) Nothing happens

What is the expected result?

When launching in headless mode and pointing to an URL with a downloadable file, file should be downloaded and saved in "Downloads" folder.

What happens instead?

Nothing happens, file doesn't get downloaded.



来源:https://stackoverflow.com/questions/46135302/chrome-headless-file-download-with-selenium-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!