python - 通过 Google Chrome 以 headless 模式下载文件

标签 python selenium google-chrome selenium-chromedriver google-chrome-headless

我在“正常”模式下在 Cromedrive 中编写代码并且工作正常。当我更改为 headless 模式时,它不会下载文件。我已经尝试过在互联网上找到的代码,但没有成功。

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。如果它在另一个驱动器中工作,那对我来说没问题。

最佳答案

首先是解决方案

Minimum Prerequisites:

要下载文件,请单击 this website 中文本为下载数据的元素,您可以使用以下解决方案:

  • 代码块:

    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:\Utility\BrowserDrivers\chromedriver.exe', service_args=["--log-path=./Logs/DubiousDan.log"])
    print ("Headless Chrome Initialized")
    params = {'behavior': 'allow', 'downloadPath': r'C:\Users\Debanjan.B\Downloads'}
    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()
    
  • 控制台输出:

    Headless Chrome Initialized
    Download button clicked
    
  • 文件下载快照:

ChromeHeadlessDownload

<小时/>

详细信息

Headless Chromium 推出以来,通过 Headless Chrome 下载文件是最受欢迎的功能之一。

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

现在,好消息是 Chromium 团队正式宣布推出通过 Headless Chromium 下载文件功能。

<小时/>

在讨论 Headless mode doesn't save file downloads @eseckler 中提到:

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.

详细讨论可以在 Issue 696481: Headless mode doesn't save file downloads 找到

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

<小时/>

[ChromeDriver] 添加了对 headless 模式下载文件的支持

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.

这是 revisioncommit

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

Resolved issue 2454: Headless mode doesn't save file downloads [Pri-2]
<小时/>

解决方案

<小时/>

片尾

然而,Mac OSX 用户需要等待 On Chromedriver, headless chrome crashes after sending Page.setDownloadBehavior on MacOSX 的馅饼。

关于python - 通过 Google Chrome 以 headless 模式下载文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58034437/

相关文章:

python计算字符串列表中的单词数

selenium - 如何杀死多个 geckodriver.exe 进程?

json - 如何在Chrome Dev Tools控制台中获取JSON输出?

google-chrome - 如何使用 webdriver 在 chrome 中打开新窗口而不是新选项卡?

testing - Selenium:遍历下拉列表中的每个 <option>

css - 为什么在 Chrome 和 Safari 中所有链接都是红色的?

google-chrome - 为什么 Angular Material Dialog 组件没有在 Google Chrome 中显示?

python - Flask-SQLAlchemy:没有外键链接这些表

python - 如何从输入和输出列表中找到函数 f?

python - 正则表达式搜索到第一个Python实例