首先解决方案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
文件下载快照:
细节
从那时起,不同的作者发表了不同的解决方案,其中一些是:
现在,好消息是Chromium团队已经正式宣布,通过Headless Chromium下载文件的功能已经到来。在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.
最后,@bugdroid修订似乎为我们解决了这个问题。在
[ChromeDriver]增加了对无头模式下载文件的支持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.
这是revision和{a12}Resolved issue 2454: Headless mode doesn't save file downloads [Pri-2]
解决方案将ChromeDriver更新到最新的ChromeDriver v77.0级别。在
注意:Chrome v77.0尚未发布/推送,因此在此之前,您可以下载并安装开发版本并从以下位置进行测试:
奥特罗