21 May 2024 Leave a comment Tech-Help
When automating web interactions using Selenium WebDriver, you may encounter the need to download files to a specific location without manually selecting the directory each time. This guide will walk you through the steps to achieve this for both Chrome and Firefox browsers.
Setting Download Location in Firefox
To set the download location in Firefox, you need to configure the Firefox profile with the appropriate preferences:
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("browser.download.folderList", 2);
profile.setPreference("browser.helperApps.neverAsk.saveToDisk", "image/jpeg");
profile.setPreference("browser.download.dir", "C:\\Users\\Admin\\Desktop\\ScreenShot\\");
WebDriver driver = new FirefoxDriver(profile);
Ensure that you set the download directory to a folder, not a specific file. Additionally, the preference key should be browser.download.folderList
with a capital ‘L’.
Setting Download Location in Chrome
For Chrome, you need to use ChromeOptions to set the download preferences:
String downloadFilepath = "C:\\Users\\Admin\\Desktop\\ScreenShot";
HashMap chromePrefs = new HashMap();
chromePrefs.put("profile.default_content_settings.popups", 0);
chromePrefs.put("download.default_directory", downloadFilepath);
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("prefs", chromePrefs);
WebDriver driver = new ChromeDriver(options);
This configuration will set the default download directory and disable the download prompt, allowing files to be saved automatically.
Handling the Save As Dialog
If you encounter a Save As dialog, you can use the Robot class to interact with the OS-level window. Here is an example:
Robot robo = new Robot();
robo.keyPress(KeyEvent.VK_ENTER);
robo.keyRelease(KeyEvent.VK_ENTER);
For more complex interactions, such as renaming the file, you can use the StringSelection
class to copy the desired file name to the clipboard and paste it using the Robot class.
StringSelection file = new StringSelection("D:\\image.jpg");
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(file, null);
Robot rb = new Robot();
rb.setAutoDelay(2000); // Similar to thread.sleep
rb.keyPress(KeyEvent.VK_CONTROL);
rb.keyPress(KeyEvent.VK_V);
rb.keyRelease(KeyEvent.VK_CONTROL);
rb.keyRelease(KeyEvent.VK_V);
rb.setAutoDelay(2000);
rb.keyPress(KeyEvent.VK_ENTER);
rb.keyRelease(KeyEvent.VK_ENTER);
Conclusion
By configuring browser profiles and leveraging tools like the Robot class, you can automate file downloads to a specific location in both Firefox and Chrome. For more advanced or large-scale automation needs, consider using a no-code tool like Repeato. Repeato offers a fast and intuitive way to create, run, and maintain automated tests for your iOS and Android applications. With features like computer vision and AI, it simplifies the testing process, ensuring your apps perform flawlessly.
For more information on using Repeato and its capabilities, visit our documentation or check out our blog.