我在“正常”模式下在 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:
- Selenium client version: Selenium v3.141.59
- Chrome version: Chrome v77.0
- ChromeDriver version: ChromeDriver v77.0
要下载文件,请单击 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
文件下载快照:
详细信息
自 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.
摘自 ChromeDriver v77.0.3865.40 (2019-08-20) 发行说明:
Resolved issue 2454: Headless mode doesn't save file downloads [Pri-2]
<小时/>
解决方案
- 将 ChromeDriver 更新到最新的 ChromeDriver v77.0 级别。
- 将 Chrome 更新至 Chrome 版本 77.0 级别。 (根据ChromeDriver v76.0 release notes)
注意:Chrome v77.0 尚未 GAed/推送发布,因此在此之前您可以从以下位置下载并安装开发版本并进行测试:
- Chrome Canary
- Dev Channel 的最新版本
片尾
然而,Mac OSX 用户需要等待 On Chromedriver, headless chrome crashes after sending Page.setDownloadBehavior on MacOSX 的馅饼。
关于python - 通过 Google Chrome 以 headless 模式下载文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58034437/