2014-09-11 70 views
0

我試圖從一個網站保存文件,但無法處理窗口對話框中的窗口 。無法控制對話框點擊保存 File.The代碼應該能夠從url下載文件。如何處理硒中的窗口對話框

我想用下面的代碼:

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

} 

}

+0

而不是使用Robot,您可以即時創建FireFoxProfile並將其配置爲自動下載到給定路徑。看到這個線程:http://stackoverflow.com/questions/1176348/access-to-file-download-dialog-in-firefox – metar 2014-09-11 13:23:02

+0

我也試過,但仍然得到對話框。我是否需要指定可下載文件的鏈接來下載它? – ninja67 2014-09-12 07:01:23

回答

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

仍然無法解決問題。對話框仍在顯示。只有download.dir正在設置目標路徑。試圖下載一個word文檔的iam是否需要提及.doc extendion在哪裏停止詢問? – ninja67 2014-09-12 09:43:47

+0

你有沒有爲'* .doc'在'browser.helperApps.neverAsk.saveToDisk'上添加正確的mimetype?你可以用'curl -I YOUR_URL_HERE |在linux上獲得mimetype grep「內容類型」' – metar 2014-09-12 10:49:29

+0

是的,這是問題...我使用mime cide for msword。 – ninja67 2014-09-12 11:24:24

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配置頁面更多的參數。

相關問題