目前selenium处理JavaScript方法很简单,只是需要调用一个方法就可以哈
accept() 点击确认按钮
dismiss() 点击取消按钮,如果没有话,不要使用这个方法,不然会报错的
这个是我早先写的项目的一个处理警告框弹框的处理方法
可以参考使用
public class alertController {
/**
* 这个代码是处理Alert
* @param webDriver
* @param operation
* @throws InterruptedException
*/
public static void doAlert(WebDriver webDriver ,String operation ) throws InterruptedException {
boolean flag = false;
Alert alert = null;
try {
new WebDriverWait(webDriver, 10).until(ExpectedConditions.alertIsPresent());
alert = webDriver.switchTo().alert();
flag = true;
} catch (NoAlertPresentException NofindAlert) {
NofindAlert.printStackTrace();
//这个进行截图功能,一个方法,如果使用代码需要把这个几行代码去掉
CameraTest cam = new CameraTest("C:\app\SimpleProtect\src\main\java\com\lsj\testgroup\photo\"+"退出登录异常", "png");
cam.snapShot();
}
if (flag && operation.equals("确认")) {
Thread.sleep(1000);
alert.accept();
}else if(flag && operation.equals("")) {
Thread.sleep(1000);
alert.accept();
}else if(flag && operation.equals("取消")) {
Thread.sleep(1000);
alert.dismiss();
}else {
CameraTest cam = new CameraTest("C:\app\SimpleProtect\src\main\java\com\lsj\testgroup\photo\"+"退出登录异常", "png");
cam.snapShot();
}
}
}
网友评论