如何处理alert、confirm、prompt对话框

 import java.io.File;

 import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver; /*
* 如何处理alert、confirm、prompt对话框
*/
public class DialogsTest { public static void main(String[] args) {
WebDriver dr = new ChromeDriver();
File file = new File("learning/Dialogs.html");
String filePath = "file:///" + file.getAbsolutePath();
System.out.println("filePath: " + filePath); dr.get(filePath);
dr.findElement(By.id("alert")).click();
Alert alert = dr.switchTo().alert();
String alertText = alert.getText();
System.out.println("alert text: " + alertText); // 点击弹出对话框的叉掉按钮
alert.dismiss(); dr.findElement(By.id("confirm")).click();
Alert confirm = dr.switchTo().alert();
String confirmText = confirm.getText();
System.out.println("confirm text: " + confirmText); // 点击弹出对话框的确定按钮
confirm.accept(); dr.findElement(By.id("prompt")).click();
Alert prompt = dr.switchTo().alert();
String promptText = prompt.getText();
System.out.println("prompt text: " + promptText);
prompt.sendKeys("tester...");
prompt.accept(); dr.quit();
} }
上一篇:Leetcode 259. 3Sum Smaller


下一篇:maven入门(上)