Home >Backend Development >Python Tutorial >How to Download Files with Chrome Headless and Selenium in Python?
Downloading Files with Chrome Headless and Selenium
In Python using Selenium and Chrome, automating file downloads can be challenging in headless mode. While downloads proceed smoothly in normal mode, headless implementations fail to initiate them. This issue persists even when a default download path is specified.
Solution:
Chromium developers have introduced a new headless mode in recent versions of Chrome, offering enhanced capabilities. This mode allows for full Chrome functionality and the use of extensions. To enable this new headless mode, add the following argument to your ChromeOptions:
<code class="python">options.add_argument("--headless=new")</code>
For Chrome versions 96 through 108, use the following argument instead:
<code class="python">options.add_argument("--headless=chrome")</code>
By incorporating this new headless mode flag, you can expect downloads to initiate normally, just as in regular Chrome. This solution resolves the issue of file downloads not starting in headless mode.
The above is the detailed content of How to Download Files with Chrome Headless and Selenium in Python?. For more information, please follow other related articles on the PHP Chinese website!