package org.coderinfo.demo;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class AlertDemo {
private static final String URL = "file:///home/moon/Desktop/alert_demo.html";
/**
* @author CoderInfo
*/
public static void main(String[] args) throws InterruptedException {
WebDriver driver = new FirefoxDriver(); //创建一个firefox的 webdriver
driver.get(URL);
driver.manage().window().maximize();
Thread.sleep(1000);
// 点击弹出alert
driver.findElement(By.id("alert")).click();
Thread.sleep(3000);
Alert alert = driver.switchTo().alert(); //捕获alert
alert.accept(); //点击确认按钮
Thread.sleep(3000); //等待3s
//点击弹出confirm
driver.findElement(By.id("confirm")).click();
Thread.sleep(3000);
Alert confirm = driver.switchTo().alert(); //捕获confirm
String confirmText = confirm.getText(); //获取confirm中的文字信息
System.out.println(confirmText);
confirm.accept(); //confirm 点击确认按钮
// confirm.dismiss(); //confirm点击取消按钮
Thread.sleep(3000);
//点击弹出prompt
driver.findElement(By.id("prompt")).click();
Thread.sleep(3000);
Alert prompt = driver.switchTo().alert(); //捕获prompt
// String promptText = prompt.getText(); //获取prompt中的文字信息
// System.out.println(promptText);
prompt.sendKeys("可能是由于太懒了"); //向prompt中输入内容
Thread.sleep(3000);
prompt.accept(); //prompt 点击确认按钮
// prompt.dismiss(); //prompt点击取消按钮
Thread.sleep(3000);
driver.quit(); // close webdriver
}
}
网友评论