[英]Chrome headless file download with Selenium in Python
In headless mode, Chrome defaults to disallowing file downloads. 在无头模式下,Chrome默认禁止文件下载。
However, recently they added an option to DevTools to enable this behavior: 但是,最近他们为DevTools添加了一个选项来启用此行为:
https://chromedevtools.github.io/devtools-protocol/tot/Page/#method-setDownloadBehavior https://chromedevtools.github.io/devtools-protocol/tot/Page/#method-setDownloadBehavior
Using Selenium with ChromeDriver in Python, how do I allow file downloads? 在Python中使用Selenium和ChromeDriver,如何允许文件下载?
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)
Here's the chromedriver ticket to add support for headless file downloads: https://bugs.chromium.org/p/chromedriver/issues/detail?id=1973 这是添加无头文件下载支持的chromedriver票: https ://bugs.chromium.org/p/chromedriver/issues/detail?id = 1973
File downloading is disabled when using current version of headless Chrome (#60). 使用当前版本的无头Chrome(#60)时,文件下载被禁用。 Support to enable downloading is added to version # 62, which is currently in Dev channel. 支持启用下载的功能已添加到版本#62,该版本目前位于Dev通道中。 Need ChromeDriver to support this feature too. 需要ChromeDriver才能支持此功能。
It also references https://bugs.chromium.org/p/chromium/issues/detail?id=696481 which has reproduction steps for the issue: 它还引用了https://bugs.chromium.org/p/chromium/issues/detail?id=696481 ,其中包含该问题的复制步骤:
Chrome Version : Chromium 58.0.3023.0 Chrome版本: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 (1)在命令行上设置无头模式( - 无头)(2)将URL指向可下载文件(3)没有任何反应
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. 在无头模式下启动并指向带有可下载文件的URL时,应下载文件并将其保存在“下载”文件夹中。
What happens instead? 会发生什么?
Nothing happens, file doesn't get downloaded. 什么都没发生,文件没有下载。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.