2015-05-13 81 views
0

我想自動化一些報告。當點擊某個元素時,它會打開彈出式對話窗口,但我不會從中獲取訪問元素。因爲代碼將在父窗口運行,它不能進入​​新打開的彈出對話框窗口,當我關閉瀏覽器時出現錯誤,但在控制檯中不會收到任何消息。如何使用硒webdriver自動彈出對話窗口

我用下面的代碼:

driver.findElement(By.id("gvWS_ctl01_ImageButton1")).click(); \\after click on this button, I won't get an access an element of popup 
dialog window and code will be running at this statement only 

driver.switchTo().frame("Weather Upload");   
driver.switchTo().activeElement();   
driver.findElement(By.id("WeatherUploadData1_btnUpload")).click(); 

回答

0

U可以訪問彈出窗口的使用警報功能,下面是示例代碼如何做到這一點。

Alert prompt = driver.switchTo().alert(); 

    // Get message in Alert pop up 
    message = prompt.getText(); 
    System.out.println("Prompt pop up message :- " + message); 

    // Force the program to wait to view the dialog box 
    try{Thread.sleep(4000);}catch(Exception e){System.out.println(e.toString());} 

    // Type value in prompt dialog box 
    prompt.sendKeys("Akshay Kumar"); 

    // Force the program to wait to view the dialog box 
    try{Thread.sleep(4000);}catch(Exception e){System.out.println(e.toString());} 

    // Click on Ok button 
    prompt.accept(); 

請讓我知道這是否可行!

+1

你不知道如果彈出一個警告或HTML頁面中選擇任何元素之前切換到彈出,運應明確規定 – Dude

0

您需要從彈出

String parentWindow = driver.getWindowHandle(); 
Set<String> windowHandles = driver.getWindowHandles(); 
Iterator<String> iterator = windowHandles.iterator(); 
while (iterator.hasNext()) { 
    String handle = iterator.next(); 
    if (!handle.contains(parentWindow)) { 
     // Switch to popup and close it 
     driver.switchTo().window(handle); 
     // Perform required action in popup 
    } 
} 
// Switching back to parent window 
driver.switchTo().window(parentWindow); 
相關問題