2013-03-22 73 views
2

我想用selenium webdriver來自動化web應用程序。所以說彈出下面的對話框,我的目標是自動點擊保存文件,然後點擊確定。Selenium Windows保存/打開對話

的Windows保存/打開文件DownloadDialouge

回答

0

不與硒可能在它自己的。

你必須與它相結合的AutoIt:

http://www.autoitscript.com/site/autoit/

(或類似的東西)。

我還想指出,大多數情況下,您只需「發送」(使用特定語言綁定中的「發送密鑰」方法/函數)文件路徑即可直接轉到上傳控件,從而確保您不要點擊實際的Browse...按鈕。一旦你這樣做了,Selenium的遊戲就結束了。

+0

謝謝Arran。我剛剛在谷歌上獲得了此鏈接。它有自動保存示例代碼http://www.autoitscript.com/wiki/Snippets_(_Files_%26_Folders_)#File_Open.2FSave.2FFolder_Dialog_Box – Jag 2013-03-22 17:50:58

2

WebDriver無法直接與對話框窗口交互,這是因爲對話框窗口是操作系統的域而不是網頁。然而其可能使用的名稱空間System.Windows.Forms的的 SendKeys類方法SendWait()對話框窗口

using System.Windows.Forms; 

在低於PLUpload按鈕示例代碼被壓做動作,這將打開一個Windows對話框來選擇要上傳的文件。

以下行用於將鍵值發送到顯示的對話窗口。

SendKeys.SendWait(@"C:\Users\Public\Pictures\Sample Pictures\Dock.jpg"); 
SendKeys.SendWait(@"{Enter}"); 

在C#SendKeys類的詳細參考可在http://msdn.microsoft.com/en-au/library/system.windows.forms.sendkeys.aspx

using System; 
using System.Windows.Forms; 
using OpenQA.Selenium.Firefox; 
using OpenQA.Selenium; 
using OpenQA.Selenium.Support; 
using OpenQA.Selenium.Interactions; 
using NUnit.Framework; 
namespace BusinessCreation 
{ 
    class PlUpload 
    { 
     static void Main(string[] args) 
     { 
       IWebDriver driver = new FirefoxDriver(); 
       driver.Navigate().GoToUrl("http://www.plupload.com/example_queuew    idget.php"); 
       driver.FindElement(By.XPath("//object[@data='/plupload/js/pluploa    d.flash.swf']")).Click(); 
       SendKeys.SendWait(@"C:\Users\Public\Pictures\Sample Pictures\Dock    .jpg"); 
       SendKeys.SendWait(@"{Enter}"); 
     } 
    } 
} 
0

找到//文件保存代碼

FirefoxProfile profile =new FirefoxProfile(); 

profile.setPreferences("browser.download.folderlist",0); 

profile.setpreferences("browser.helperApps.neverAsk.saveToDisk",mimeType);//applicatio/zip 

WebDriver driver = new FireforDriver(profile); 

driver.get("http:seleniumhq.org/download"); 

driver.get("http://selenium.com/files/selenium.zip"); 

//文件打開代碼

profile.setPreferences("browser.helperApps.neverAsk.open",mimeType); 
0

對文件han使用此方法危及周圍:

我們需要:

jacob.jar Download

它將包含一個jar文件和2個.dll文件

AutoItX4Java.jar Download

public static void uploadFile(String path, String browser){ 

if(browser.equalsIgnoreCase("chrome")){ 

    if(x.winWaitActive("Open", "", 10)){ 
     if(x.winExists("Open")){ 
      x.sleep(500); 
      x.send(path); 
      x.controlClick("Open", "", "Button2"); 

     } 
    } 

} 


if(browser.equalsIgnoreCase("firefox")){ 

    if(x.winWaitActive("File Upload", "", 10)){ 
     if(x.winExists("File Upload")){ 
      x.sleep(500); 
      x.send(path); 
      x.controlClick("File Upload", "", "Button2"); 

     } 
    } 
} 

if(browser.equalsIgnoreCase("InternetExplorer")){ 

    if(x.winWaitActive("Choose File to Upload", "", 10)){ 
     if(x.winExists("Choose File to Upload")){ 
      x.sleep(500); 
      x.send(path); 
      x.controlClick("Choose File to Upload", "", "Button2"); 

     } 
    } 
} 



} 


public void test(){ 
    //Click on the Select button of the file upload 
    uploadFile("Path", "chrome"); 


} 

謝謝.. 。不要單擊Accept或Upvote,直到它適用於您。如果它不適合你的意思,請評論.. Dont Downvote ...