如何在selenium中处理窗口对话框

时间:2014-09-11 12:20:21

标签: java firefox selenium

我正在尝试从网站保存文件但无法处理Windows 对话框。无法控制对话框以单击“保存” 文件。代码应该能够从网址下载文件。

我正在尝试使用以下代码:

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();
    }

}

}

2 个答案:

答案 0 :(得分:0)

您应该设置FF配置文件以保存failes而不使用对话框:

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.

查看firefox中的about:config页面以获取更多参数。

答案 1 :(得分:0)

这是一个包含一些示例mimetypes的完整工作配置。确保添加了适合您文件的正确文件

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");