Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.6k views
in Technique[技术] by (71.8m points)

google chrome - Download files in Java, Selenium using ChromeDriver and headless mode

As it is still not clear for me how to download files using --headless mode in ChromeDriver - selenium [Java], add here please the example of how to do so, I try to do it like that (the file downloading works properly without --headless option):

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 since Chrome v60+ by setting Browser.setDownloadBehaviour(true, _DIRECTORY) but I cant find the information whether ChromeDriver already supports it or it is just me using wrong chrome preferences as arguments

ChromeDriver version: 2.34 Selenium + WebDriver version: 3.8.1

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...