Java代码
package com.test;
import java.util.List;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;
public class Test_Dialogs {
public static void main(String[] args) {
String url = "file:///C:/Documents and Settings/fei yong/桌面/t.html";
//打开chrome
WebDriver driver = new ChromeDriver();
driver.get(url);
//点击第一个按钮,输出对话框上面的文字,然后叉掉
driver.findElement(By.id("alert")).click();
Alert alert = driver.switchTo().alert();
String text = alert.getText();
System.out.println("alert:"+text);
alert.dismiss();
//点击第二个按钮,输出对话框上面的文字,然后点击确认
driver.findElement(By.id("confirm")).click();
Alert confirm = driver.switchTo().alert();
String text1 = confirm.getText();
System.out.println("confirm:"+text1);
confirm.accept();
//点击第三个按钮,输入你的名字,然后点击确认,最后
driver.findElement(By.id("prompt")).click();
Alert prompt = driver.switchTo().alert();
String text2 = prompt.getText();
System.out.println("prompt:"+text2);
prompt.sendKeys("fei");
prompt.accept();
//driver.quit();
}
}