alert、confirm、prompt这样的js对话框在selenium1 时代处理起来比价麻烦,常常要用autoit来帮助处理。
而现在webdriver对这些弹出框做了专门的处理,使用selenium2处理对话框就变得十分方便简洁。
alert、confirm、prompt 不是 JavaScript 核心的函数。
alert 是 BOM 中的成员函数,具体说是 window.alert。
所以说,alert对话框显示在最前,并且禁止了浏览器页面其他的操作。废话不多说,看看怎么用webdriver操作这三种弹出框。
一、操作 Alert 弹框
alert在网页的HTML代码:
οnclick='alert("this is a [alert] window!");'/>
当我们点击这个按钮的时候,就会弹出alert弹框:
package com.automation.alert;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.NoAlertPresentException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
/**
* 类说明:操作alert弹框
*
* @version 1.0
* 2016年11月19日 下午9:47:12
*/
public class AlertDemo {
private static WebDriver driver = null ;
private static String chromeDriverDir = "D:\\workspace\\A_Test\\resource\\chromedriver.exe";
public static void main(String[] args) {
//1.打开浏览器;
System.setProperty(