0

I am trying to save a file from a website but unable to handle windows dialogue box. not able to get control of dialogue box to click on Save File.The code should be able to download the file from url.

I am trying with following code:

package new;
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.io.BufferedReader;
import java.io.InputStreamReader;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile; 
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.support.ui.WebDriverWait;

public class BlipsManagement 
{
    static WebDriver driver;
public static void main(String args[]) throws AWTException 
{
    //System.setProperty("webdriver.ie.driver","D:\\IEDriverServer.exe");              
    //driver= new InternetExplorerDriver();
    driver= new FirefoxDriver();

    try
    {
        String EIN="xyz";
        String pass="abc";
        String st_date="01/08/2014";
        String end_date="10/09/2014";
        String URL="http://myurl.com";
        driver.get(URL);
        Thread.sleep(2000);
        //url opens
        driver.findElement(By.name("Logon")).click();
        driver.switchTo().activeElement().sendKeys(EIN);
        driver.switchTo().activeElement().sendKeys(Keys.TAB);
        driver.switchTo().activeElement().sendKeys(pass);
        driver.switchTo().activeElement().sendKeys(Keys.ENTER);
        Thread.sleep(2000);

        //element is located

        driver.findElement(By.name("YES")).click();
        Thread.sleep(3000);
        driver.findElement(By.linkText("locator")).click();
        Thread.sleep(3000);
        driver.findElement(By.linkText("locator")).click();

        Thread.sleep(3000);

        //entries are passed

        driver.findElement(By.id("PeriodStartDate")).clear();
        driver.findElement(By.id("PeriodStartDate")).click();
        driver.findElement(By.id("PeriodStartDate")).sendKeys(st_date.toString());


        driver.findElement(By.id("PeriodFinishDate")).clear();
        driver.findElement(By.id("PeriodFinishDate")).click();
        driver.findElement(By.id("PeriodFinishDate")).sendKeys(end_date.toString());

        driver.findElement(By.name("locator")).clear();
        driver.findElement(By.name("locator")).sendKeys("text");

        //checkboxes selected
        driver.findElement(By.name("PM1")).click();
        driver.findElement(By.name("locator")).click();

        driver.findElement(By.cssSelector("input.formButton")).click();

        System.out.println("Generated");
        Thread.sleep(3000);
        //click the word image 
        driver.findElement(By.linkText("My Reports")).sendKeys(Keys.TAB);
        driver.switchTo().activeElement().sendKeys(Keys.ENTER);

        //here is where the problem is
        //the dialogue box of firefox opens and asks whether to open file or save file.
        //initially open file radio button is selected to select save file alt+s is used
        //still not working

        driver.switchTo().activeElement();
        Robot robot = new Robot();
        robot.keyPress(KeyEvent.VK_ALT);

        robot.keyPress(KeyEvent.VK_S);


        robot.keyRelease(KeyEvent.VK_S);

        robot.keyRelease(KeyEvent.VK_ALT);

        //robot.keyPress(KeyEvent.VK_ENTER);
        //robot.keyRelease(KeyEvent.VK_ENTER);*/

        driver.switchTo().activeElement().click();

        System.out.println("done!");

    }
    catch (InterruptedException e) 
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

}

ninja67
  • 25
  • 2
  • 6
  • instead of using Robot you could create a FireFoxProfile on the fly and configure it to autodownload to a given path. See this thread: http://stackoverflow.com/questions/1176348/access-to-file-download-dialog-in-firefox – metar Sep 11 '14 at 13:23
  • i tried that too but still getting the dialogue box. do i need to specify the link of the downloadable file to download it??? – ninja67 Sep 12 '14 at 07:01

2 Answers2

0

You should to set FF profile to save failes without Dialog box:

profile.SetPreference("browser.download.lastDir", Settings.TempDir); // dir to save files profile.SetPreference("browser.helperApps.neverAsk.saveToDisk", @"application/vnd.ms-excel, text/html"); // file MIME types to save without asking.

Look at the about:config page in your firefox for more params.

VovecUdalec
  • 151
  • 1
  • 8
0

here is a complete working configuration with some sample mimetypes. make sure you have added the correct ones suitable for your files

FirefoxProfile fp = new FirefoxProfile();
fp.setPreference("browser.download.folderList", 2);
fp.setPreference("browser.download.manager.showWhenStarting", false);
fp.setPreference("browser.download.dir", "path_to_file");
fp.setPreference("browser.helperApps.neverAsk.saveToDisk", "text/html,text/x-csv,application/x-download,application/vnd.ms-excel,application/pdf,application/msexcel");
metar
  • 434
  • 7
  • 17
  • still not able to handle to solve the issue. the dialogue box is still showing. only the download.dir is setting the destination path. iam trying to download a word document does it need to mention .doc extendion anywhere to stop asking ? – ninja67 Sep 12 '14 at 09:43
  • have you added the correct mimetype to `browser.helperApps.neverAsk.saveToDisk` for that *.doc? you could get the mimetype on linux with `curl -I YOUR_URL_HERE | grep "Content-Type"` – metar Sep 12 '14 at 10:49
  • yes that was the issue... i was using mime cide for msword. – ninja67 Sep 12 '14 at 11:24