Aakash Singh
Aakash Singh

Reputation: 57

Not able to download file in chrome(59 Latest) to a specific directory without any window popup using Selenium WebDriver

I need to download a file in chrome browser(59 Latest Version) to a specfic directory without having window popup to show up. Using the below code it shows the window popup. If I don't use this the file will be downloaded to downloads folder without showing any windows popup. I have seen a lot of people faced the similar issue but this code worked well for them. Is it some issue with latest Chrome?

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

        DesiredCapabilities cap = DesiredCapabilities.chrome();
        cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
        cap.setCapability(ChromeOptions.CAPABILITY, options);

        driver = new ChromeDriver(cap);

Upvotes: 1

Views: 1091

Answers (1)

undetected Selenium
undetected Selenium

Reputation: 193088

As I tested this functionality with Selenium 3.4.0, ChromeDriver 2.30 & Chrome 59.0, I have tried with your own code to download a excel file from the url https://www.microsoft.com/en-in/download/details.aspx?id=45485 along with some simple tweaks. The code block works fine at my end.

System.setProperty("webdriver.chrome.driver", "C:\\Utility\\BrowserDrivers\\chromedriver.exe");
String downloadFilepath = "C:\\Utility\\OP_Resources\\ChromeDownload";
HashMap<String, Object> chromePrefs = new HashMap<String, Object>();
chromePrefs.put("profile.default_content_settings.popups", 0);
chromePrefs.put("download.prompt_for_download", "false");
chromePrefs.put("download.default_directory", downloadFilepath);
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("prefs", chromePrefs);
options.addArguments("start-maximized");
options.addArguments("disable-infobars");
DesiredCapabilities cap = DesiredCapabilities.chrome();
cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
cap.setCapability(ChromeOptions.CAPABILITY, options);
WebDriver driver = new ChromeDriver(cap);
driver.get("https://www.microsoft.com/en-in/download/details.aspx?id=45485");
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("window.scrollBy(0,200)", "");
driver.findElement(By.linkText("Download")).click();

Upvotes: 0

Related Questions