5

I used this code for download file , but its not working

FirefoxProfile profile = new FirefoxProfile();

profile.setPreference("browser.download.dir","D:\\WebDriverDownloads");
profile.setPreference("browser.download.folderList", 2);
profile.setPreference("browser.helperApps.neverAsk.saveToDisk","application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;"); 
profile.setPreference( "browser.download.manager.showWhenStarting",false );
profile.setPreference( "pdfjs.disabled",true );


FirefoxDriver driver = new FirefoxDriver(profile);  //Shows error on this line

driver.get("http://toolsqa.com/automation-practice-form/");

driver.findElement(By.linkText("Test File to Download")).click();

Thread.sleep(5000);

It gives me error

Error message

And when I remove

'Profile'

form this FirefoxDriver driver = new FirefoxDriver(profile); then code run successfully but download files window is not closing and file also not downloading.

Instead of this I use Robot

Robot object=new Robot();
object.keyPress(KeyEvent.VK_DOWN);
object.keyRelease(KeyEvent.VK_DOWN);         
object.keyPress(KeyEvent.VK_ENTER);
object.keyRelease(KeyEvent.VK_ENTER);

and its working fine.But Why my above code is not working?

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Pradnya Bolli
  • 1,915
  • 1
  • 19
  • 37
  • @DebanjanB it gives same error message which shows in image , in this code also have `WebDriver driver = new FirefoxDriver(profile);` line thats why it showing error – Pradnya Bolli Apr 09 '19 at 10:42

1 Answers1

3

To download the file clicking on the link with text as Test File to Download you need to:

  • Create a new FirefoxProfile() and set the required preferences.
  • Use an instance of FirefoxOptions() set the profile.
  • You can use the following solution:

    System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe");
    FirefoxProfile profile = new FirefoxProfile();
    profile.setPreference("browser.download.dir", "C:\\Utility\\Downloads");
    profile.setPreference("browser.download.folderList",2);
    profile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
    profile.setPreference("browser.download.manager.showWhenStarting", false);
    profile.setPreference("browser.helperApps.neverAsk.openFile","application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
    profile.setPreference("browser.helperApps.alwaysAsk.force", false);
    profile.setPreference("browser.download.manager.useWindow", false);
    profile.setPreference("browser.download.manager.focusWhenStarting", false);
    profile.setPreference("browser.download.manager.showAlertOnComplete", false);
    profile.setPreference("browser.download.manager.closeWhenDone", true);
    FirefoxOptions options = new FirefoxOptions();
    options.setProfile(profile);
    WebDriver driver = new FirefoxDriver(options);
    driver.get("http://toolsqa.com/automation-practice-form/");
    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.linkText("Test File to Download"))).click();
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • 1
    Thanks for solution!!! its working ... but can you give little bit explanation about code? – Pradnya Bolli Apr 09 '19 at 11:19
  • We have just added the required _preference_ not to show the download popup through a _FirefoxProfile_ and baked it into an instance of _FirefoxOptions()_ which we have passed into the `FirefoxDriver(options)` constructor. Yes, as per best practices, you need to induce _WebDriverWait_ for the desired _elementToBeClickable()_. Any specific line you want me to explain? – undetected Selenium Apr 09 '19 at 11:23
  • Thanks for explanation... we use `profile.setPreference("browser.helperApps.neverAsk.openFile", "");` this line thats why it not showing save window ? – Pradnya Bolli Apr 09 '19 at 11:27
  • Corrected it right now, this property should be assigned a boolean value of `true`. – undetected Selenium Apr 09 '19 at 11:28
  • It is there twice now, once with a list of MIME types, and once with the boolean value of `true`. Either way, those options don't help and it is just not working. – Daniel Ziltener Jan 08 '20 at 11:28
  • @DanielZiltener That was my bad, corrected now, let me know the status. – undetected Selenium Jan 08 '20 at 11:38
  • It is as non-functional as it ever was :) – Daniel Ziltener Jan 09 '20 at 16:33