-
-
Notifications
You must be signed in to change notification settings - Fork 8.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Download file in headless chrome #5159
Comments
Apparently, this new command needs to be added to all the bindings. |
I managed to get it working by issuing the above request directly to the |
This is how I did it: ChromeDriverService driverService = ChromeDriverService.createDefaultService();
ChromeDriver driver = new ChromeDriver(driverService, options);
Map<String, Object> commandParams = new HashMap<>();
commandParams.put("cmd", "Page.setDownloadBehavior");
Map<String, String> params = new HashMap<>();
params.put("behavior", "allow");
params.put("downloadPath", downloadFilepathString);
commandParams.put("params", params);
ObjectMapper objectMapper = new ObjectMapper();
HttpClient httpClient = HttpClientBuilder.create().build();
String command = objectMapper.writeValueAsString(commandParams);
String u = driverService.getUrl().toString() + "/session/" + driver.getSessionId() + "/chromium/send_command";
HttpPost request = new HttpPost(u);
request.addHeader("content-type", "application/json");
request.setEntity(new StringEntity(command));
httpClient.execute(request); |
The method send commands to the browser to avoid blocking of downloading in headless mode. (The issue is described here: SeleniumHQ#5159)
Hello, Any plans to support setDownloadBehavior in order to download files using chrome in headless mode? Any doc/examples in .Net would be much appreciated. Regards, |
Even I am facing this same issue |
of downloading in headless mode. (The issue is described here: SeleniumHQ#5159)
of downloading in headless mode. (The issue is described here: SeleniumHQ#5159)
Has anyone got this working with Ruby/Rails? Also, can you get a development build of chrome on Heroku? |
@p0deje, @jonhkr Could you please verify this spec, it is failing for me: pulkitsharma07@1142e0b#diff-b1ade8967288d0002579a914345a9f56R72 For me even https://github.com/SeleniumHQ/selenium/blob/master/javascript/node/selenium-webdriver/test/chrome/devtools_test.js#L51 is failing. I am using Chrome 64 and chromedriver 2.35, not sure what could be causing this. |
I've popped an optimisation in, also, whilst not having worked in this area of the specs. Is there a reason you verbosely create the driver, there are helpers such as |
No particular reason, I missed that. The branch is not yet ready, Will do the changes to use But, still, the main issue is that I am not able to download files in headless mode. |
Is there an example for .NET? |
@pulkitsharma07 I cannot make it work too, maybe something has changed in recent Chrome/ChromeDriver? |
So, in my headless tests Chrome is crashing on clicking the element to download the file, whereas it is not crashing with the same scenario in normal mode. chromedriver logs:
|
@p0deje I have filed https://bugs.chromium.org/p/chromedriver/issues/detail?id=2270. Please take a look. |
This adds commands to hit the send_command and send_command_and_get_result endpoints Addresses SeleniumHQ#5159
Upgrading to Chrome 65/chromedriver 2.36 fixed my crash issue with headless chrome. |
Here is a .NET implementation to enable downloads using headless chrome: ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.AddArgument("--headless");
ChromeDriver chromeDriver = new ChromeDriver(chromeOptions);
Dictionary<string, object> commandParameters = new Dictionary<string, object>
{
["behavior"] = "allow",
["downloadPath"] = @"C:\Downloads"
};
chromeDriver.ExecuteChromeCommand("Page.setDownloadBehavior", commandParameters); |
This fix is not working with my configuration. I'm still unable to download a file in headless mode. |
public static WebDriver getChromeDriver() throws IOException {
|
@rishi1992 did you found any solution can you please share. |
@amishad02 I use the following successfully
|
Anyone had success sending the |
This has worked for me, // Set the ChromeDriver path
|
Any good solution/workaround in Python ? |
Please check, if there is a new tab is opened, when you click on a download link (for example if has target="_blank" attribute). In my case download in headless with above solution doesn't work for downloads in a new tabs. So you can remove target="_blank" attribute by JS or get href and try to download by direct link in the same tab. This works for me. |
to enable headless downloads in Python:
|
Thanks cgoldberg - that works fine! |
@cgoldberg What is your env? Chrome and chromedriver versions, standalone selenium or Grid? Thanks |
@cgoldberg I am using this similar procedure with selenium-webdriver for Node and it does not work for me. I suspect that it is because in my case a new tab is opened to download file ( Does this happen to anyone else? |
Is there any way to make this permanent? That is, I want it to be enabled by default when a new instance of the browser is opened. |
Make use of Hooks |
An example I call from my hooks is 👍 |
The command does not seem to be working now in Chrome 74+ (using Proctractor 5.4.2 and node 10.16.0)
Fails with the error
Any idea why it is failing or how to get headless Chrome downloads working again? |
Because the endpoint for that command changed in chromedriver to |
@lmtierney many thanks for this. I am still getting a 500 internal server error now if I use that endpoint uri instead?
|
I agree. My Chrome version is "78.0.3904.70". This doesnt work for me either. I have the same code |
This worked for me: Environmentruby 2.4.0p0 (2016-12-24 revision 57164) [x86_64-darwin18] Other resourceshttps://stackoverflow.com/questions/48810757/setting-default-download-directory-and-headless-chrome Implementationoptions = Selenium::WebDriver::Chrome::Options.new
options.add_argument('--window-size=1500,1500')
options.add_argument('--headless')
options.add_argument('--no-sandbox')
options.add_argument('--disable-gpu')
options.add_argument('--disable-popup-blocking')
options.add_preference(:download,
directory_upgrade: true,
prompt_for_download: false,
default_directory: ENV['downloads_folder']
)
options.add_preference(:browser, set_download_behavior: { behavior: 'allow' })
Capybara.register_driver :headless_chrome do |app|
driver = Capybara::Selenium::Driver.new(
app,
browser: :chrome,
options: options
)
bridge = driver.browser.send(:bridge)
path = '/session/:session_id/chromium/send_command'
path[':session_id'] = bridge.session_id
bridge.http.call(:post, path, cmd: 'Page.setDownloadBehavior',
params: {
behavior: 'allow',
downloadPath: ENV['downloads_folder'],
})
driver
end
Capybara.javascript_driver = ENV['capybara_js_driver'].to_sym |
I'm trying to get this working on javascript using nodejs. Is there a solution yet? |
I've found a solution to this in Node.JS. You need to set the download path through the
|
Closing as this went stale. If the issue still persists, please open an issue with the ChromeDriver team. |
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
Meta -
OS: OSX
Selenium Version: 3.8.1
Browser: Chrome
Expected Behavior -
Permit files to be downloaded in headless mode.
Actual Behavior -
Files aren't downloaded.
Steps to reproduce -
Launch chrome in headless mode and try to download any file.
Looks like it is a feature to prevent sites from downloading files when running chrome in headless mode.
https://bugs.chromium.org/p/chromium/issues/detail?id=696481
To permit downloads it's necessary to send a command to chrome.
I'm using java and I couldn't find a way to make it work.
I even tried sending a post request to
/session/:sessionId/chromium/send_command
but that didn't work.The text was updated successfully, but these errors were encountered: