Loading file from Selenium and chromedriver
I cannot get Selenium and Chrome (Canary) to download the file. I am using Java and Chrome 59/60 (because my tests are for both Windows and Linux) and I am trying to run a file download from a webpage.
When I, from selenium, DO NOT set the headless mode, the chrome window opens and the file loads.
When I set the flag --headless
, the chrome window does not open and the download does not start.
public static void chromeDownload() throws IOException, InterruptedException{
ChromeOptions options = new ChromeOptions();
String downloadFilepath = "";
if (ValidateOS.isWindows()){
System.out.println("This is a Windows system.");
System.setProperty("webdriver.chrome.driver", "resources\\driver\\chromedriver.exe");
options.setBinary("C:\\Users\\Juri\\AppData\\Local\\Google\\Chrome SxS\\Application\\chrome.exe");
downloadFilepath = "C:\\";
} else if (ValidateOS.isUnix()){
System.out.println("This is a Unix system.");
System.setProperty("webdriver.chrome.driver", "resources/driver/chromedriver");
options.setBinary("/usr/bin/google-chrome");
downloadFilepath = "/home/juri/";
}
// Manage the download
HashMap<String, Object> chromePrefs = new HashMap<>();
chromePrefs.put("profile.default_content_settings.popups", 0);
chromePrefs.put("download.default_directory", downloadFilepath);
// Save Chrome Options
HashMap<String, Object> chromeOptionsMap = new HashMap<>();
options.setExperimentalOption("prefs", chromePrefs);
options.addArguments("--headless --disable-gpu");
DesiredCapabilities cap = DesiredCapabilities.chrome();
cap.setCapability(ChromeOptions.CAPABILITY, chromeOptionsMap);
cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
cap.setCapability(ChromeOptions.CAPABILITY, options);
ChromeDriver driver = new ChromeDriver(cap);
driver.get("http://localhost/my-test-page.html");
driver.findElement(By.id("download")).click();
Thread.sleep(5000); // wait 5 seconds for a small file to download.. yes.. I know...
driver.quit();
}
When pressed , in GUI mode the download starts. This is not the case in Headless mode.
How to solve?
OT
I am using Chrome Canary which on my v.60 comes with - headless . Ultra easy to run grabber on server without gui. But for the same reason .. It is useless for me to load Chrome on a server without a GUI. Apart from the main question. I wonder if you developers think that installing a server on a Linux server is just okay to run it in headless mode.
Update : I am still looking for a solution, in case anyone ever read this: / There are several search results and I tried all of them
source to share
Solved by adapting the code found from this link: File uploads in Java, Selenium using ChromeDriver mode and headless mode
For anyone wondering how my code is now ...
public static void chromeDownload(String address, String Headless, String DownDir) throws IOException, InterruptedException{
ChromeOptions options = new ChromeOptions();
String downloadFilepath = DownDir;
if (ValidateOS.isWindows()){
System.out.println("This is a Windows system.");
System.setProperty("webdriver.chrome.driver", "resources\\driver\\chromedriver.exe");
//options.setBinary("C:\\Users\\Juri\\AppData\\Local\\Google\\Chrome SxS\\Application\\chrome.exe");
// If this is commented, the grabber will use the main Chrome
} else if (ValidateOS.isUnix()){
System.out.println("This is a Unix system.");
System.setProperty("webdriver.chrome.driver", "resources/driver/chromedriver");
options.setBinary("/usr/bin/google-chrome");
}
switch (Headless.toUpperCase()){
case "TRUE":
options.addArguments("--headless --disable-gpu");
break;
case "FALSE":
default:
options.addArguments("--window-size=1152,768");
break;
}
options.addArguments("--test-type");
options.addArguments("--disable-extension");
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", downloadFilepath);
params.put("cmd", "Page.setDownloadBehavior");
commandParams.put("params", params);
ObjectMapper objectMapper = new ObjectMapper();
CloseableHttpClient 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(address);
driver.findElement(By.id("download")).click();
driver.quit();
}
source to share
Are you expecting to use Docker? Run dockerized Ubuntu with selenium grid and any number of browsers. Or just a browser without Selenium grid.
You wouldn't need to use headless mode, on the other hand, you could multithread. Example:
-
Download the file .
-
Then just run using the following command:
docker-compose up -d
-
several settings on your server to access the network server on "localhost"
http://localhost:4444/grid/console
http://localhost:4444/wd/hub
-
Use the following code:
WebDriver Driver = new RemoteWebDriver (new URL (" http: // localhost: 4444 / wd / hub ")); // handle the exception in the URL constructor ().
-
you have lightweight virtual machines with browsers of any size that your server can handle. And no need for mode
--headless
This is not a solution to the initial problem, though.
source to share