2012-09-28 130 views

回答

7

等待警報沒有默認的方法。

但是,你可以寫下你自己的方法。

waitForAlert(WebDriver driver) 
{ 
    int i=0; 
    while(i++<5) 
    { 
     try 
     { 
      Alert alert = driver.switchTo().alert(); 
      break; 
     } 
     catch(NoAlertPresentException e) 
     { 
      Thread.sleep(1000); 
      continue; 
     } 
    } 
} 
-1
public boolean isAlertPresent() { 

    boolean presentFlag = false; 

    try { 

    // Check the presence of alert 
    Alert alert = driver.switchTo().alert(); 
    // Alert present; set the flag 
    presentFlag = true; 
    // if present consume the alert 
    alert.accept(); 

    } catch (NoAlertPresentException ex) { 
    // Alert not present 
    ex.printStackTrace(); 
    } 

    return presentFlag; 

} 
+1

討論這種方法不會等待警報。只是它會檢查警報是否存在。 – Santoshsarma

+0

對不起。是的。得到一個律,但misconfused。 試試這個: http://stackoverflow.com/questions/8310548/how-can-i-reliably-wait-for-javascript-alerts-using-selenium2-webdriver –

相關問題