通过Google Chrome以无头模式下载文件
我在“正常”模式下在Cromedrive中做代码,并且效果很好。当我更改为无头模式时,它不会下载文件。我已经尝试了我找到的Alround Internet的代码,但没有起作用。
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。如果在另一个驱动器中起作用,对我来说很好。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
首先是解决方案
下载文件单击该元素的文本,at text as 下载在 //www.mockaroo.com/“ rel =“ noreferrer”>此网站您可以使用以下解决方案:
代码块:
控制台输出:
文件下载快照:
通过
无头铬是自无头铬。
从那时起,有不同贡献者发表了不同的工作,其中一些是:
现在,好消息是 chromium 团队已正式宣布了功能通过
无头铬
下载文件的到来。在讨论中无头模式不保存文件下载 href =“ https://bugs.chromium.org/u/1164142555/” rel =“ noreferrer”>@eseckler 提到:
可以在
最后,@bugdroid 修订似乎已经为我们确定了这个问题。
[Chromedriver]增加了对无头模式下载文件的支持
这是
rel =“ noreferrer”> chromedriver v77.0.3865.40(2019-08-20)发行说明:
解决方案
eutro
mac osx 用户可以等待他们的pie,as 在Chromedriver上,在macOSX上发送Page.set downloadbehavior后,无头Chrome崩溃。
First the solution
To download the file clicking on the element with text as Download Data within this website you can use the following solution:
Code Block:
Console Output:
File Downloading snapshot:
Details
Downloading files through Headless Chromium was one of the most sought functionality since Headless Chrome was introduced.
Since then there were different work-arounds published by different contributors and some of them are:
Now the, the good news is Chromium team have officially announced the arrival of the functionality Downloading file through
Headless Chromium
.In the discussion Headless mode doesn't save file downloads @eseckler mentioned:
A detailed discussion can be found at Issue 696481: Headless mode doesn't save file downloads
Finally, @bugdroid revision seems to have nailed the issue for us.
[ChromeDriver] Added support for headless mode to download files
Here is the revision and commit
From ChromeDriver v77.0.3865.40 (2019-08-20) release notes:
Solution
Note: Chrome v77.0 is yet to be GAed/pushed for release so till then you can download and install a development build and test either from:
Outro
However Mac OSX users have a wait for their pie as On Chromedriver, headless chrome crashes after sending Page.setDownloadBehavior on MacOSX.
chomedriver版本:95.0.4638.54
Chrome版本95.0.4638.69
似乎有效的是我使用“ \\”而不是“/”用于地址。后一种方法没有丢下任何错误,但也没有下载任何文档。但是,使用双重斜线完成了这项工作。
Chomedriver Version: 95.0.4638.54
Chrome Version 95.0.4638.69
What seemed to work was that I used "\\" instead of "/" for the address. The latter approach didn't throw any error, but didn't download any documents either. But, using double back slashes did the job.
对于JavaScript使用以下代码:
然后单击“下载按钮:”,请切换选项卡
For javascript use below code:
Then switch tabs as soon as you click the download button:
此c#对我有用
注意,请注意新的无头选项 https:// www .selenium.dev/blog/2023/headless-is-going-away/
This C# works for me
Note the new headless option https://www.selenium.dev/blog/2023/headless-is-going-away/
解决方案:
我尝试了其他一切。加上这条线对我有用。
Solution:
I tried everything else. Adding that line is what worked for me.
我认为您不应该使用浏览器下载内容,将其留给Chrome Developers/Testers。
我相信您应该宁愿获得 href属性使用请求库
如果您的网站需要身份验证,则可以获取 cookies 来自浏览器实例,并将其传递给 requests.session 。
I don't think you should be using the browser for downloading content, leave it to Chrome developers/testers.
I believe you should rather get href attribute of the element you want to download and obtain it using requests library
If your site requires authentication you could fetch cookies from the browser instance and pass them to requests.Session.