Selenium: Change Default Download Directory For Firefox Browser

Selenium: Change Default Download Directory For Firefox Browser

When we hit that download button in every browser, a download popup appears, and the file gets saved in a default location called downloads. This is a pretty common scenario for download and its download path.

What if I want the download to happen in another place?

  • Selenium download path is also the same as the local download path it saves in the downloads folder.
  • Selenium can change the default download directory.
  • When done manually, it is useless as the driver always opens up with default settings.
  • It won't be the same as when we made the change.
  • So it has to be in the programming level change.
  • Let's say I want the file in my project directory.

How can we do that?

We can achieve this by using the Firefox Profile class.

Today for this article purpose, we will download a jar file and save it in our project folder where drivers are present.

public class AppTest 
{
    WebDriver driver;
	
    @BeforeTest
    public void bt()
    {	System.setProperty("webdriver.gecko.driver",System.getProperty("user.dir")+File.separator+"drivers"+File.separator+"geckodriver");
       
        FirefoxProfile profile = new FirefoxProfile();
        profile.setPreference("browser.download.folderList", 2);
        profile.setPreference("browser.download.dir",System.getProperty("user.dir") + File.separator + "downloads");
        profile.setPreference("browser.helperApps.neverAsk.saveToDisk","text/csv,application/zip");

        FirefoxOptions option = new FirefoxOptions();
        option.setProfile(profile);

        driver = new FirefoxDriver(option);  
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(30));
    }
	
    @Test
    public void shouldAnswerWithTrue()
    {
        driver.get("http://autopract.com/selenium/download.html");
        driver.findElement(By.cssSelector(".mydownload")).click();;
    }
}
  • First, we are initializing the gecko driver path, the name of the firefox driver we call in selenium.
  • Then we create the profile object for the firefox profile.
  • Using this profile, we set up the preferences we need for the customized download.
  • In the first preference, we ask firefox/gecko drivers to use the custom download path with argument 2. Here 0: the desktop. 1 (default): the downloads folder. 2: the last folder specified for a download
  • Next, we set the property or the file path location to which the files should be saved. In this case, we are holding it in our project.
  • Then we are setting preference for file formats to avoid download prompt, which usually occurs in Firefox. After that specify, A comma-separated list of MIME types to save to disk without asking what to use to open the file. Default value is an empty string. You can check the mime type here 
  • And using the options object we create afterward, we pass in the profile object.
  • Once we click download now, the options object, which we can call firefox options, directly maps to the profile path we provided and save the downloaded file into that path.
  • File.separator is used as this will support other OS platforms too.

Tags
Standard (Image)