Uploading files in Java, Selenium using ChromeDriver mode and bezgons
As it is still not clear to me how to download files using --headless mode in ChromeDriver - selenium [Java], please add here an example of how to do it, I am trying to do it like this (file upload works correctly without option " Headless" ):
ChromeOptions lChromeOptions = new ChromeOptions();
HashMap<String, Object> lChromePrefs = new HashMap<String, Object>();
lChromePrefs.put("profile.default_content_settings.popups", 0);
lChromePrefs.put("download.default_directory", _PATH_TO_DOWNLOAD_DIR);
lChromePrefs.put("browser.set_download_behavior", "{ behavior: 'allow' , downloadPath: '"+_PATH_TO_DOWNLOAD_DIR+"'}");
lChromeOptions.addArguments("--headless");
lChromeOptions.addArguments("--disable-gpu");
lChromeOptions.setExperimentalOption("prefs", lChromePrefs);
WebDriver lWebDriver = new ChromeDriver(lChromeOptions);
From what I know downloading files in headless mode should be possible with Chrome v60 + by installing Browser.setDownloadBehaviour(true, _DIRECTORY)
, but I can't seem to find information on whether ChromeDriver supports it or is just using the wrong chrome preferences as arguments
ChromeDriver version: 2.34 Selenium + WebDriver version: 3.8.1
source to share
In Java, use like this:
System.setProperty("webdriver.chrome.driver", "/usr/local/bin/chromedriver");
ChromeOptions options = new ChromeOptions();
options.addArguments("--test-type");
options.addArguments("--headless");
options.addArguments("--disable-extensions"); //to disable browser extension popup
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", "//home//vaibhav//Desktop");
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);
driver.get("http://www.seleniumhq.org/download/");
driver.findElement(By.linkText("32 bit Windows IE")).click();
source to share
Hi I tried to implement the solution mentioned by @Vaibhav Sharma above but it didn't work for me. Even when I tried to do the exact same thing he did, that is, download selenium, still headless, it didn't work.
The code I tried to download with selenium is the same as above and post it. Please let me know if I have done anything wrong.
package start;
import java.util.HashMap;
import java.util.Map;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClientBuilder;
import org.openqa.selenium.By;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeDriverService;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.*;
import com.fasterxml.jackson.databind.ObjectMapper;
public class Test_Download {
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
System.setProperty("webdriver.chrome.driver","C:\\developmenttools\\START\\webdrivers\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.addArguments("--test-type");
options.addArguments("--headless");
options.addArguments("--disable-extensions"); //to disable browser extension popup
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", "//Downloads");
commandParams.put("params", params);
ObjectMapper objectMapper = new ObjectMapper();
HttpClient httpClient = HttpClientBuilder.create().build();
String command = objectMapper.writeValueAsString(commandParams);
String u = driverService.getUrl().toString() + "/session/" + ((RemoteWebDriver)driver).getSessionId() + "/chromium/send_command";
System.out.println("command= "+command);
HttpPost request = new HttpPost(u);
request.addHeader("content-type", "application/json");
request.setEntity(new StringEntity(command));
httpClient.execute(request);
Thread.sleep(5000);
driver.get("http://www.seleniumhq.org/download/");
driver.findElement(By.linkText("32 bit Windows IE")).click();
}catch(Exception exception){
}
}
}
I want to download pdf in headless mode prefs.put("plugins.always_open_pdf_externally", true);
, it works fine using prefs.put("plugins.always_open_pdf_externally", true);
but not in headless mode.
source to share