0

Using Selenium Java in my automation framework and trying to download PDF from Chrome, below is my code:

        System.setProperty("webdriver.chrome.driver", "resources/drivers/chromedriver.exe");
        
        ChromeOptions options = new ChromeOptions();
        
        HashMap<String, Object> chromePrefs = new HashMap<String, Object>();
        chromePrefs.put("profile.default_content_settings.popups", 0);
        chromePrefs.put("plugins.always_open_pdf_externally", true);
        chromePrefs.put("download.default_directory", "C:");
        options.setExperimentalOption("prefs", chromePrefs);
        
        driver = new ChromeDriver(options);

I specified location "C:" (for just testing purpose) but the issue is that it downloads PDFs in Downloads folder.

Also is there a way to specify name of file that I'm trying to download?

Ihor Harmatii
  • 189
  • 3
  • 21
  • https://stackoverflow.com/questions/34515328/how-to-set-default-download-directory-in-selenium-chrome-capabilities has a similar answer. – Arundeep Chohan Sep 10 '20 at 23:25
  • @arundeep-chohan as you can see my code looks almost the same but it still saves files not in specified directory – Ihor Harmatii Sep 11 '20 at 01:15

2 Answers2

1

I solved the problem, the issue was it is not recommended to use folders like: "C:" or "Desktop" or relative path, that's why it didn't work.

Ihor Harmatii
  • 189
  • 3
  • 21
0

Try this, it should work :

String desired_path = "D:\\user\\report";
HashMap hm = new HashMap();
hm.put("download.default_directory",desired_path);
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("prefs",hm);
WebDriver driver = new ChromeDriver(options);
Tushar
  • 1