在Python中使用Selenium下载Chrome无头文件

2024-07-08 10:25:06 发布

您现在位置:Python中文网/ 问答频道 /正文

在无头模式下,Chrome默认不允许文件下载。在

但是,最近他们在DevTools中添加了一个选项来启用此行为:

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

在Python中使用Selenium和ChromeDriver,如何允许文件下载?在


Tags: 文件httpsiogithub选项page模式chrome
2条回答

对于那些仍在寻找的人,我就是这样做的:

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)

以下是chromedriver添加对无头文件下载的支持: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.

它还引用了https://bugs.chromium.org/p/chromium/issues/detail?id=696481,其中包含问题的复制步骤:

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.

相关问题 更多 >

    热门问题