如何检查是否存在警报,如果是,则接受警报

时间:2014-02-12 13:35:31

标签: java selenium

我有一种情况,我想检查是否存在弹出窗口,如果是,则接受它,否则继续前进。请帮助我,因为我是selenium的新手。我正在使用java。感谢。

2 个答案:

答案 0 :(得分:1)

这将是这样的。

WebDriverWait wait = new WebDriverWait(driver, 10 /*timeout in seconds*/);
                           if(wait.until(ExpectedConditions.alertIsPresent())==null){
                                 System.out.println("alert was not present");
                           }
                           else
                           {
                            Alert alert = driver.switchTo().alert();
                           alert.accept();
                           System.out.println("alert was present and accepted");
                           }

答案 1 :(得分:0)

我想这可能是你:

@Test
public void testAlertOk()
{
//Now we would click on AlertButton
WebElement button = driver.findElement(By.id("AlerButton"));
button.click();
try {
//Now once we hit AlertButton we get the alert
Alert alert = driver.switchTo().alert();
//Text displayed on Alert using getText() method of Alert class
String AlertText = alert.getText();
//accept() method of Alert Class is used for ok button
alert.accept();
//Verify Alert displayed correct message to user
assertEquals("this is alert box",AlertText);
} catch (Exception e) {
e.printStackTrace();
}
}

来源:Click here for more detail understanding

相关问题