Sumanth Kumar Reddy
Sumanth Kumar Reddy

Reputation: 11

Changing Download Location in Google Chrome using Selenium Webdriver

I have tried the following code:

ChromeOptions profile=new ChromeOptions();
profile.addArguments("disable-popup-blocking", "true");
profile.addArguments("download.default_directory","D:\\WORKSPACE\\SeConnect\\Downloads\\");
profile.addArguments("download.directory_upgrade", "true");
profile.addArguments("download.prompt_for_download", "false");
driver = new ChromeDriver(profile);

But it is not setting the path. It is using default path.

Upvotes: 1

Views: 4495

Answers (1)

Sumanth Kumar Reddy
Sumanth Kumar Reddy

Reputation: 11

driver.get("chrome://settings/advanced");
            JavascriptExecutor js = (JavascriptExecutor) driver;
            String prefId = "download.default_directory";
            File tempDir=new File(System.getProperty("user.dir")+"\\Downloads\\");
            if (driver.findElements(By.xpath(String.format(".//input[@pref='%s']", prefId))).size() == 0) {
                driver.get("chrome://settings-frame");
                driver.findElement(By.xpath(".//button[@id='advanced-settings-expander']")).click();
            }
            String tmpDirEscapedPath = tempDir.getCanonicalPath().replace("\\", "\\\\");
            js.executeScript(String.format("Preferences.setStringPref('%s', '%s', true)", prefId,
                    tmpDirEscapedPath));

Upvotes: 0

Related Questions