2013-10-01 123 views
2

如何使用selenium webdriver處理文件窗口彈出窗口。如何使用selenium webdriver處理Windows文件瀏覽窗口

我已經點擊文件瀏覽按鈕,新彈出的窗口已經打開,我unabled處理窗口(因爲我要選擇的文件)

WebElement browser=driver.findElement(By.name("uploadFile")); 
browser.click(); 
driver.switchTo().window("File Upload"); 
WebElement el=driver.findElement(By.name("fileName")); 
el.sendKeys("E:\\DVBScheduleEvent.xml"); 
+0

不要點擊上傳按鈕。只需要執行'driver.findElement(By.name(「uploadFile」))。sendKeys(「E:\\ DVBScheduleEvent.xml」);'' – Arran

回答

2

我對此有一個示例代碼:

driver.get("http://www.2shared.com/"); 
     driver.findElement(By.id("upField")).sendKeys("D:\\james.xls"); 
     driver.findElement(By.xpath("//input[@title='Upload file']")).click(); 
    } 
1

使用此方法用於文件處理:

我們需要:

Ĵ acob.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"); 


    } 

謝謝...不要單擊接受或給予好評,直到它爲你工作。如果它不爲你工作方式,請評論..不要Downvote ...

0

此代碼爲我工作

WebElement filepath=driver.findElement(By.id("fileUploadId")); 
filepath.sendKeys("C:\\TextFile.txt"); 
相關問題