Search code examples
javaseleniumgoogle-chromedownloadheadless

How to download files headless in Selenium (Java) when download happens in new tab?


I have a web page where when I click a button it opens another tab and then in it, downloads a csv file after few seconds. I was trying to automate this headlessly but I am unable to do so. I am using the below code. But I think the below solutions is for download happening in same window. How can I tweak it to work in my situation?

The code works fine and the file gets download if i run this normally (non headless) by commenting out the line options.addArguments("--headless");

    System.setProperty("webdriver.chrome.driver", webdriverpath);
    ChromeOptions options = new ChromeOptions();
    options.setExperimentalOption("useAutomationExtension", false);
    options.addArguments("--test-type");
    options.addArguments("--headless");
    options.addArguments("--disable-extensions"); 
    ChromeDriverService driverService = ChromeDriverService.createDefaultService();


    HashMap<String, Object> chromePrefs = new HashMap<String, Object>();
    chromePrefs.put("profile.default_content_settings.popups", 0);
    chromePrefs.put("download.default_directory", downloadFilepath);
    options.setExperimentalOption("prefs", chromePrefs);


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




    //OPEN URL
    //Click Button

Solution

  • Here is the solution that worked for in Java :

    Right after clicking the download button switch to new tab and also do a refresh :

        ArrayList<String> tabs2 = new ArrayList<String> (driver.getWindowHandles());
        driver.switchTo().window(tabs2.get(1));
        driver.navigate().refresh();
    

    (In my case it worked without the refresh. So you mey try without that statement aswell)