python chrome headless_通过googlechrome在headless mod中下载文件

首先解决方案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

文件下载快照:

c2e315791ac8ec2c929e52f9c806c060.png

细节

从那时起,不同的作者发表了不同的解决方案,其中一些是:

现在,好消息是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尚未发布/推送,因此在此之前,您可以下载并安装开发版本并从以下位置进行测试:

奥特罗

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值