下面代码中介绍了告警框的处理方法

package com.test.alerthandle;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.TimeoutException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class AlterHandle {

    public static void main(String[] args) {
        WebDriver driver = new FirefoxDriver();
        driver.get("file:///D:/10-selenium/workspace/SeleniumTest/src/com/test/alerthandle/alert.html");
        driver.manage().window().maximize();

        driver.findElement(By.cssSelector("#altertest")).click();

        try {
            // 先等待prompt框的出现,然后输入内容
            new WebDriverWait(driver, 5).until(ExpectedConditions.alertIsPresent());
            driver.switchTo().alert().sendKeys("处理告警框的例子");

            // 确认输入内容
            waitTime(3000);
            driver.switchTo().alert().accept();

            // 获取Alert框内text内容
            waitTime(2000);
            new WebDriverWait(driver, 5).until(ExpectedConditions.alertIsPresent());
            String inputInfo = driver.switchTo().alert().getText();
            System.out.println(inputInfo);

            // 关闭Alert框
            waitTime(3000);
            driver.switchTo().alert().accept();

            // 利用js构造一个confirm框
            waitTime(3000);
            String js = "confirm(\"这就是一个告警框的例子\")";
            ((JavascriptExecutor) driver).executeScript(js);

            // 取消confirm框
            waitTime(3000);
            driver.switchTo().alert().dismiss();

            waitTime(3000);
            driver.quit();

        } catch (TimeoutException e) {
            driver.quit();
        }

    }

    static public void waitTime(int time) {

        try {
            Thread.sleep(time);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}

实例例子:

 

<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<title>Alter</title>
<link
    href="http://cdn.bootcss.com/bootstrap/3.3.0/css/bootstrap.min.css"
    rel="stylesheet" />
</head>
<body>
    <button id="altertest" onclick="disp_alert()">this is an alter</button>
    <script type="text/javascript">
        function disp_alert() {
            var a=prompt("请输入信息:","");            
            alert("你输入的信息是:" + a );
        }
    </script>
</body>
<script
    src="http://cdn.bootcss.com/bootstrap/3.3.0/css/bootstrap.min.js"></script>
</html>