如何处理alert、confirm、prompt对话框
1 import java.io.File; 2 3 import org.openqa.selenium.Alert; 4 import org.openqa.selenium.By; 5 import org.openqa.selenium.WebDriver; 6 import org.openqa.selenium.chrome.ChromeDriver; 7 8 /* 9 * 如何处理alert、confirm、prompt对话框 10 */ 11 public class DialogsTest { 12 13 public static void main(String[] args) { 14 WebDriver dr = new ChromeDriver(); 15 File file = new File("learning/Dialogs.html"); 16 String filePath = "file:///" + file.getAbsolutePath(); 17 System.out.println("filePath: " + filePath); 18 19 dr.get(filePath); 20 dr.findElement(By.id("alert")).click(); 21 Alert alert = dr.switchTo().alert(); 22 String alertText = alert.getText(); 23 System.out.println("alert text: " + alertText); 24 25 // 点击弹出对话框的叉掉按钮 26 alert.dismiss(); 27 28 dr.findElement(By.id("confirm")).click(); 29 Alert confirm = dr.switchTo().alert(); 30 String confirmText = confirm.getText(); 31 System.out.println("confirm text: " + confirmText); 32 33 // 点击弹出对话框的确定按钮 34 confirm.accept(); 35 36 dr.findElement(By.id("prompt")).click(); 37 Alert prompt = dr.switchTo().alert(); 38 String promptText = prompt.getText(); 39 System.out.println("prompt text: " + promptText); 40 prompt.sendKeys("tester..."); 41 prompt.accept(); 42 43 dr.quit(); 44 } 45 46 }