A2oz

How to Set Download Location in Firefox Using Selenium Java?

Published in Selenium WebDriver 2 mins read

You can set the download location in Firefox using Selenium Java by configuring the browser's profile preferences. Here's how:

1. Create a Firefox Profile

  • Use the FirefoxProfile class in Selenium to create a new Firefox profile.
  • Set the browser.download.folderList preference to 2 (for the custom location) and the browser.download.dir preference to the desired download location.
  • Create a new Firefox driver instance using the created profile.

2. Code Example

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.openqa.selenium.firefox.FirefoxProfile;

public class FirefoxDownloadLocation {

    public static void main(String[] args) {

        // Set the desired download location
        String downloadLocation = "C:\\Downloads";

        // Create a Firefox profile
        FirefoxProfile profile = new FirefoxProfile();

        // Set download preferences
        profile.setPreference("browser.download.folderList", 2);
        profile.setPreference("browser.download.dir", downloadLocation);

        // Configure Firefox options
        FirefoxOptions options = new FirefoxOptions();
        options.setProfile(profile);

        // Create a new Firefox driver instance
        WebDriver driver = new FirefoxDriver(options);

        // Continue with your Selenium test automation
        // ...

        // Close the browser
        driver.quit();
    }
}

3. Explanation

  • The FirefoxProfile class allows you to customize the Firefox browser settings.
  • browser.download.folderList determines the download directory type. 2 indicates a custom location.
  • browser.download.dir specifies the actual path to the custom download location.

4. Additional Considerations

  • Ensure that the specified download location exists and is writable.
  • You can also use the browser.helperApps.neverAsk.saveToDisk preference to automatically save certain file types without prompts.

Note: This approach works with Firefox versions up to version 75. For newer versions, you might need to use different methods or explore alternative solutions.

Related Articles