I am trying to automate file download using Selenium.
Whenever a file is received for download, i want to save that particular file to custom location and save it with custom name.
I want browser to ask to save each file so that i can provide custom path and file name dynamically.
I am able to save the file to custom directory but i could not get control on the file name. I want to use java.awt.Robot
, java.awt.datatransfer.StringSelection
and java.awt.Toolkit
for using custom file name.
My code
ChromeOptions chromeOptions = new ChromeOptions();
HashMap<String, Object> chromePreferences = new HashMap<String, Object>();
chromePreferences.put("profile.default_content_settings.popups", 0);
chromePreferences.put("download.default_directory", browserDownloadDirectoryPath);
chromePreferences.put("safebrowsing.enabled", "true");
chromeOptions.setExperimentalOption("prefs", chromePreferences);
chromeOptions.addArguments("--test-type");
chromeDesiredCapabilities = DesiredCapabilities.chrome();
chromeDesiredCapabilities.setCapability(ChromeOptions.CAPABILITY, chromeOptions);
This is saving file to custom folder.
If the browser ask for file saving, i want to use Robot Class to send path.
StringSelection stringSelection = new StringSelection(
"<file path>" + "<file name>");
Toolkit.getDefaultToolkit().getSystemClipboard()
.setContents(stringSelection, null);
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_TAB);
robot.keyRelease(KeyEvent.VK_TAB);
robot.keyPress(KeyEvent.VK_DOWN);
robot.keyRelease(KeyEvent.VK_DOWN);
robot.keyPress(KeyEvent.VK_DOWN);
robot.keyRelease(KeyEvent.VK_DOWN);
robot.keyPress(KeyEvent.VK_TAB);
robot.keyRelease(KeyEvent.VK_TAB);
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
Kindly provide a solution to force browser to ask to save file.
For Firefox, we have about:config
to see all preferences for the browser. Do we have something like this for other browsers as well?