弹出对话框的处理

JavaScript共有3中弹出对话框Alert、Confirmation以及Prompt。
  1. Alert:警告对话框,只有一个确定的按钮
  2. Confirmation:确认对话框,一般是一个确定和取消按钮
  3. Prompt:输入对话框,需要输入内容
方法:
  • Accept() 单击弹出对话框的确认按钮
  • Dismiss() 单击弹出对话框的取消按钮
  • SendKeys(keysToSend) 在弹出对话框中输入文本,该方法值对Prompt弹出对话框有效
  • getText() 用于获取弹出对话框的文本内容
 
例子:
public static void main(String[] args) throws InterruptedException {
 
  System.setProperty("webdriver.firefox.bin", "F:\firefox\firefox.exe");
  WebDriver driver = new FirefoxDriver();
  driver.navigate().to("file:///C:/Users/yunling/Desktop/testPage.html");
  System.out.println("正确获取URL: "+driver.getCurrentUrl().equals("file:///C:/Users/yunling/Desktop/testPage.html"));
 
  driver.findElement(By.xpath("html/body/input[1]")).click();
  Thread.sleep(2000);
  System.out.println(driver.switchTo().alert().getText()); //输出获取弹出对话框的内容
  driver.switchTo().alert().accept(); //单击对话框的确定按钮
  driver.findElement(By.xpath("html/body/input[2]")).click();
  Thread.sleep(2000);
  driver.switchTo().alert().dismiss(); //单击对话框的取消按钮
  driver.findElement(By.xpath("html/body/input[3]")).click();
  Thread.sleep(2000);
  System.out.println(driver.switchTo().alert().getText());
  driver.switchTo().alert().sendKeys("这就是输入的内容");
  driver.switchTo().alert().accept();
}
原文地址:https://www.cnblogs.com/wysk/p/7511880.html