0

I am trying to automate a download process in my application.

The last step is a dialog box.

I want Selenium to select 'Save File' radio button in the dialog box and then click 'OK' button.

Is this possible in Selenium Webdriver?

Note: 'Alert' didn't work.

Its neither a Window nor an Alert, and so I'm not able to switch to this dialog box.

Any help would be much appreciated.

Dialog box which I see is same as

WebDriver driver = new FirefoxDriver(); driver.get("http://selenium-release.storage.googleapis.com/2.47/selenium-java-2.47.1.zip");

I've tried getWindowHandle() method to switch to the dialog box. But it didn't work

SP.
  • 85
  • 3
  • 13
  • Share your code which you have tried so far. – Musakkhir Sayyed Aug 18 '15 at 11:15
  • Send keys should work with dilogue box array, but sharing code is best practice. – MarmiK Aug 18 '15 at 11:36
  • Is the dialog box a System dialog or an HTML dialog? Can you right click on the dialog and see the typical menu items that you would find if you right clicked on a web page? If so, it's an HTML dialog and can be interacted with like any other page. We need more info to help you. Can you share a link to the page or the HTML that launches that dialog? – JeffC Aug 18 '15 at 17:52
  • @JeffC, The dialog box is a windows based dialog and not an HTML dialog. The dialog box which I see is same as WebDriver driver = new FirefoxDriver(); driver.get("http://selenium-release.storage.googleapis.com/2.47/selenium-java-2.47.1.zip"); – SP. Aug 19 '15 at 04:49
  • @Musakkhir Sayyed I tried switching to the dialog box with the code (String childWindow1 : driver.getWindowHandles()) { driver.switchTo().window(childWindow1); } . But this is not working because the dialog box is not a separate window. The dialog box which I see is same as The dialog box which I see is same as WebDriver driver = new FirefoxDriver(); driver.get("http://selenium-release.storage.googleapis.com/2.47/selenium-java-2.47.1.zip"); – SP. Aug 19 '15 at 04:59

4 Answers4

1

You can use this code to download a file, here all the popups and alerts are set so you can automate your download process:

public class fileUse {

    public static String downloadPath = "D:\\downloads";

    @Test
    public void testDownload() throws Exception {
        WebDriver driver = new FirefoxDriver(FirefoxDriverProfile());   
        driver.manage().window().maximize();
        driver.get("FILE URL");
    }

    public static FirefoxProfile FirefoxDriverProfile() throws Exception {
        FirefoxProfile profile = new FirefoxProfile();
        profile.setPreference("browser.download.folderList", 2);
        profile.setPreference("browser.download.manager.showWhenStarting", false);
        profile.setPreference("browser.download.dir", downloadPath);
        profile.setPreference("browser.helperApps.neverAsk.openFile",
                "text/csv,application/x-msexcel,application/excel,application/x-excel,application/vnd.ms-excel,image/png,image/jpeg,text/html,text/plain,application/msword,application/xml");
        profile.setPreference("browser.helperApps.neverAsk.saveToDisk",
"text/csv,application/x-msexcel,application/excel,application/x-excel,application/vnd.ms-excel,image/png,image/jpeg,text/html,text/plain,application/msword,application/xml");
        profile.setPreference("browser.helperApps.alwaysAsk.force", false);
        profile.setPreference("browser.download.manager.alertOnEXEOpen", false);
        profile.setPreference("browser.download.manager.focusWhenStarting", false);
        profile.setPreference("browser.download.manager.useWindow", false);
        profile.setPreference("browser.download.manager.showAlertOnComplete", false);
        profile.setPreference("browser.download.manager.closeWhenDone", false);
        profile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/zip");
            return profile;
        }
    }

Do not forget to import the org.testng.annotations.Test library

  • Hi Aakash, the radio button is in a windows based dialog box, something like a 'Print' dialog box. And so this is not a web element and cannot be accessed by driver.findElement() method – SP. Aug 19 '15 at 05:07
  • yes you can use it .. just add switch window query and you are done .. you can add switch window which switches the current execution window and then you can use this driver.findElement() method .. check the answer I've updated it] – Aakash Shah Aug 19 '15 at 05:15
  • It throws NoSuchWindowException because the dialog box here is not a separate window. You can see the dialog box if you run the code I've given in my updated question – SP. Aug 19 '15 at 06:22
  • This is not working for my application. I'm still getting the same dialog box. I tested the code in another website and it worked. But it's not working for mine! :( – SP. Aug 19 '15 at 08:07
  • mail me the details, I'll try to solve your problem – Aakash Shah Aug 19 '15 at 08:11
0

Actually It is browser dependent and you are using Firefox

Use firefox profile to download your files. This profile skip that dialoge box of firefox. In line:-

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

The value of browser.download.folderList can be set to either 0, 1, or 2. When set to 0, Firefox will save all files downloaded via the browser on the user's desktop. When set to 1, these downloads are stored in the Downloads folder. When set to 2, the location specified for the most recent download is utilized again.

Firefox profile code that you need to implement :-

        FirefoxProfile pro=new FirefoxProfile();
        pro.setPreference("browser.downLoad.folderList", 0);
        pro.setPreference("browser.helperApps.neverAsk.saveToDisk", "Applications/zip");
        WebDriver driver=new FirefoxDriver(pro);
        driver.get("http://selenium-release.storage.googleapis.com/2.47/selenium-java-2.47.1.zip");

Hope it will help you :)

Shubham Jain
  • 16,610
  • 15
  • 78
  • 125
0

Download alert is windows based alert. It is not put up by browser. So, you can't automate it using Selenium WebDriver. You can use AutoIT or Robot framework for automating the windows based popups.

Harish
  • 306
  • 1
  • 4
  • 14
-1

Please use Auto IT tool and automate the download window part through it. Invoke the AutoIT Sciprt in your script using javaScriptExecutor.

Hope it will work!