1

A reporting application has an Export to Excel link which upon clicking opens a OS popup similar to the one shown below.

Internet Explorer

Export OS Popup

Firefox

enter image description here

I need to click on the Save As(IE) or Save File(FF) button and provide the path I need in the 'Save As' popup window and save the file. I'm aware of the driver.getWindowHandle() functionality but I'm not able to handle OS popups using this method.

Please let me know if there is anyway in Selenium or in Java to handle OS popups in any browser like the above ones.

EDIT: I believe answers to this question is restricted to Firefox. What I am looking for is a solution for handling this kind of native OS popups that works across all browsers (at least in IE, Chrome and Firefox)

Community
  • 1
  • 1
justcurious
  • 839
  • 3
  • 12
  • 29
  • possible duplicate of [How to download any file and save it to the desired location using Selenium Webdriver](http://stackoverflow.com/questions/16746707/how-to-download-any-file-and-save-it-to-the-desired-location-using-selenium-webd) – JeffC Sep 15 '15 at 22:19
  • You will have to handle this for each browser dependent on the browser because Selenium doesn't handle anything outside of the webpage. – JeffC Sep 15 '15 at 22:20

3 Answers3

0

You need use Firefox profiling to overcome from this issue:-

        FirefoxProfile pro=new FirefoxProfile();

        pro.setPreference("browser.downLoad.folderList", 0);

        pro.setPreference("browser.helperApps.neverAsk.saveToDisk", "Applications/zip");

        WebDriver driver=new FirefoxDriver(pro);
  • browser.download.folderList controls the default folder to download a file to. 0 indicates the Desktop; 1 indicates the systems default downloads location; 2 indicates a custom folder.

  • browser.download.dir holds the custom destination folder for
    downloading. It is activated if browser.download.folderList has been set to 2.

  • browser.helperApps.neverAsk.saveToDisk stores a comma-separated list of MIME types to save to disk without asking what to use to open the file.

Below code for chrome multiple download option:-

    ChromeOptions options = new ChromeOptions();
    Map<String, Object> prefs = new HashMap<String, Object>();
    prefs.put("profile.default_content_settings.popups", 0);
    prefs.put("download.default_directory", getClass().getResource("/data/input").toString().replace("%20", " ").replace("file:","").replaceFirst("/", ""));
    options.setExperimentalOption("prefs", prefs);

    options.addArguments("--test-type");
    capabilities.setCapability(ChromeOptions.CAPABILITY, options);

For IE Refer :-

http://9to5it.com/internet-explorer-disable-do-you-want-to-open-or-save-this-file-prompt/

You can also use Robot class of java but it can lock your screen for while

You need to use ROBOT class for firing an ENTER Action event. In java if you want to fire any event you have to use Robot class for typing using programatically or firing events like ENTER and ESCAPE.

// Create object of Robot class
Robot object=new Robot();

// Press Enter
object.keyPress(KeyEvent.VK_ENTER);

// Release Enter
object.keyRelease(KeyEvent.VK_ENTER);

Hope it will help you :)

Please get back to me if still facing issue :)

Shubham Jain
  • 16,610
  • 15
  • 78
  • 125
  • Thanks for answering. I'm not able to try your answer since I'm having an issue with my FF browser. Once it is fixed, I will let you know whether it works. Meanwhile It will be helpful if you know any methods or APIs that will help me handle these popups across all browsers – justcurious Sep 15 '15 at 12:40
  • once you initialize firefox with the above profile it will be set for throughout the session, so you don't need to worry at each download action :) – Shubham Jain Sep 15 '15 at 12:46
  • 1
    That's fine. By 'across browsers' I meant, a solution that works in IE, Chrome and Firefox. – justcurious Sep 15 '15 at 13:09
  • Chrome browser will be a better approach since the Chrome will not open the file download pop-up.But multiple download option should be enabled if you need multiple downloads. – Shubham Jain Sep 15 '15 at 13:14
  • I'm not really worried about multiple downloads. I will just put it straight to you - Are there any methods to handle these kind of native OS popups, independent of the type of browser used? – justcurious Sep 15 '15 at 13:27
  • I have updated my code. Did you downvote me? I need to delete this answer if yes – Shubham Jain Sep 15 '15 at 13:38
  • Absolutely not. Why would I down vote you? At least you cared to provide a valid solution to my question rather than **trying** to find duplicates or finding faults like some ppl here ;) – justcurious Sep 15 '15 at 13:48
  • +1 for the edited solution. Will try out your suggestions and accept your answer if it works for me. Thanks :) – justcurious Sep 15 '15 at 13:49
0

I think above answer is useful only when we have to Save the file / download the file on local machine. Operating system's native dialogs such as Save As, Open are not controllable by selenium web driver. I use AutoIT (only for Windows OS)to handle windows specific pop-ups. You may refer AutoIT for initial understanding. So here, invoke autoIT script when you click on any Web Element that will handle further OS specific operations such as Saving file, browsing file to upload etc.

MKay
  • 818
  • 9
  • 32
  • Can you please elaborate on using AutoIT (probably with an example) for handling these popups? – justcurious Sep 15 '15 at 12:41
  • Assuming you have gone through above link to understand what AutoIT is. Consider here [Example of Selenium+AutoIT](http://seleniumeasy.com/selenium-tutorials/upload-a-file-using-selenium-webdriver-with-autoit) – MKay Sep 16 '15 at 03:59
  • Sorry to redirect you on other websites. I would have given my code but I am not good at editing that in comments ;). Plus that link will provide better understanding :) Also, that is just an example. You will find several blogposts providing code samples for using AutoIT. – MKay Sep 16 '15 at 04:11
0

Try Below code. It worked for me :

        FirefoxProfile profile = new FirefoxProfile();

        profile.setPreference("browser.helperApps.neverAsk.saveToDisk",
                "application/octet-stream;application/csv;text/csv;application/vnd.ms-excel;");
        profile.setPreference("browser.helperApps.alwaysAsk.force", false);
        profile.setPreference("browser.download.manager.showWhenStarting", false);
        profile.setPreference("browser.download.folderList", 2);
        profile.setPreference("browser.download.dir", "C:\\SampleExcel");
        driver = new FirefoxDriver(profile);  

Enjoy :)

whatsinthename
  • 1,828
  • 20
  • 59