基于Selenium2+Java的UI自动化(6)-操作Alert、confirm、prompt弹出框

时间:2023-02-07 17:38:34

alert、confirm、prompt这样的js对话框在selenium1 时代处理起来比价麻烦,常常要用autoit来帮助处理。
而现在webdriver对这些弹出框做了专门的处理,使用selenium2处理对话框就变得十分方便简洁。

alert、confirm、prompt 不是 JavaScript 核心的函数。

alert 是 BOM 中的成员函数,具体说是 window.alert。

所以说,alert对话框显示在最前,并且禁止了浏览器页面其他的操作。废话不多说,看看怎么用webdriver操作这三种弹出框。

一、操作 Alert 弹框


alert在网页的HTML代码:

<input id="alert" type='button' value='alert'
onclick='alert("this is a [alert] window!");'/>

当我们点击这个按钮的时候,就会弹出alert弹框:

基于Selenium2+Java的UI自动化(6)-操作Alert、confirm、prompt弹出框


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弹框
* <br/>
* @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("webdriver.chrome.driver", chromeDriverDir);
driver = new ChromeDriver();
driver.manage().window().maximize(); //打开文件网址;
driver.get("file:///E:/desktop/upload.html"); //定位alert按钮对象;
WebElement alertButton = driver.findElement(By.id("alert")); //点击alert按钮,弹出alert弹出框
alertButton.click(); try {
//获取Alert弹框对象;
Alert alertWindow = driver.switchTo().alert(); //获取alert弹框的文本,并打印到控制台;
String alertText = alertWindow.getText();
System.out.println(alertText); //点击alert弹出框中的确定按钮;
alertWindow.accept();
} catch (NoAlertPresentException e) {
System.out.println("尝试操作的alert弹出框未找到!");
e.printStackTrace();
} }
}

二、操作confirm弹框


confirm在网页的HTML代码:

<input id="confirm" type='button' value='confirm'
onclick='confirm("this is a [confirm] window!");'/>

点击confirm按钮的是,弹出confirm弹框,这个弹框有两个按钮,一个确定按钮,一个取消按钮;

基于Selenium2+Java的UI自动化(6)-操作Alert、confirm、prompt弹出框


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; /**
* 类说明:操作confirm弹框
* <br/>
* @version 1.0
* 2016年11月19日 下午9:46:47
*/
public class ConfirmDemo {
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("webdriver.chrome.driver", chromeDriverDir);
driver = new ChromeDriver();
driver.manage().window().maximize(); //打开文件网址;
driver.get("file:///E:/desktop/upload.html"); //定位confirm按钮对象;
WebElement confirmButton = driver.findElement(By.id("confirm")); //点击confirm按钮,弹出confirm弹出框
confirmButton.click(); //获取Alert弹框对象;
Alert confirmWindow = null; try {
confirmWindow = driver.switchTo().alert(); //获取confirm弹框的文本,并打印到控制台;
String confirmText = confirmWindow.getText();
System.out.println(confirmText); //点击confirm弹出框中的确定按钮;
confirmWindow.accept(); //重新操作,点击confirm弹框的取消按钮;
confirmButton.click();
confirmWindow = driver.switchTo().alert();
confirmWindow.dismiss(); } catch (NoAlertPresentException e) {
System.out.println("尝试操作的confirm弹出框未找到!");
e.printStackTrace();
}
}
}

三、操作prompt弹框


prompt在网页的HTML代码:

<input id="prompt" type='button' value='prompt'
onclick='prompt("this is a [prompt] window!","1111");'/>

点击prompt按钮的是,弹出prompt弹框,这个弹框有两个按钮、一个输入框;

基于Selenium2+Java的UI自动化(6)-操作Alert、confirm、prompt弹出框
QQ图片20161119215628.png705x240 12.7 KB

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; /**
* 类说明:操作prompt弹框
* <br/>
* @version 1.0
* 2016年11月19日 下午10:02:42
*/
public class PromptDemo {
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("webdriver.chrome.driver", chromeDriverDir);
driver = new ChromeDriver();
driver.manage().window().maximize(); //打开文件网址;
driver.get("file:///E:/desktop/upload.html"); //定位prompt按钮对象;
WebElement promptButton = driver.findElement(By.id("prompt")); //点击prompt按钮,弹出prompt弹出框
promptButton.click(); //获取Alert弹框对象;
Alert promptWindow = null; try {
promptWindow = driver.switchTo().alert(); //获取prompt弹框的文本,并打印到控制台;
String confirmText = promptWindow.getText();
System.out.println(confirmText); //向prompt弹框中的输入框对象,输入文本;
promptWindow.sendKeys("selenium + webdriver!"); //点击prompt弹出框中的确定按钮;
promptWindow.accept(); //重新操作,点击prompt弹框的取消按钮;
promptButton.click();
promptWindow = driver.switchTo().alert();
promptWindow.dismiss(); } catch (NoAlertPresentException e) {
System.out.println("尝试操作的prompt弹出框未找到!");
e.printStackTrace();
}
}
}

从以上代码可以看出driver.switchTo().alert();这句可以得到 alert \ confirm \ prompt 对话框的对象,然后运用其方法对它进行操作。对话框操作的主要方法有:

getText() 得到它的文本值
accept() 相当于点击它的"确认"
dismiss() 相当于点击"取消"或者叉掉对话框
sendKeys() 输入值,这个alert\confirm没有对话框就不能用了,不然会报错。

我们专注于持续集成,更多原创请关注:www.hordehome.com

基于Selenium2+Java的UI自动化(6)-操作Alert、confirm、prompt弹出框的更多相关文章

  1. 基于js alert confirm样式弹出框

    基于js alert confirm样式弹出框.这是一款根据alert confirm优化样式的确认对话框代码. 在线预览   源码下载 实现的代码. html代码: <div id=&quot ...

  2. 基于Selenium2&plus;Java的UI自动化&lpar;4&rpar; - WebDriver API简单介绍

    1. 启动浏览器 前边有详细介绍启动三种浏览器的方式(IE.Chrome.Firefox): private WebDriver driver = null; private String chrom ...

  3. 基于Selenium2&plus;Java的UI自动化&lpar;1&rpar; - 原理和环境搭建

    一.Selenium2的原理 Selenium1是thoughtworks公司的一个产品经理,为了解决重复烦躁的验收工作,写的一个自动化测试工具,其原理是用JS注入的方 式来模拟人工的操作,但是由于J ...

  4. 基于Selenium2&plus;Java的UI自动化(8)- 显式等待和隐式等待

    一.隐式等待 package com.automation.waits; import java.util.concurrent.TimeUnit; import org.openqa.seleniu ...

  5. 基于Selenium2&plus;Java的UI自动化&lpar;5&rpar; - 执行JavaScript脚本

    一.操作日期选择框 QQ图片20161118215530.png1336x545 22.6 KB 说明:日期选择框大部分是不支持前端输入的,因为这个对象是 readOnly,只读属性,selenium ...

  6. 基于Selenium2&plus;Java的UI自动化&lpar;2&rpar; - 启动浏览器

    一.准备工作 我们常用的浏览器主要有三个:chrome.Firefox.IE:其中chrome 和 IE 需要下载驱动程序,才能启动浏览器,注意驱动程序有32位和64位两种. 另外:如何查看本机的浏览 ...

  7. 基于Selenium2&plus;Java的UI自动化&lpar;3&rpar; - 页面元素定位

    一.几种网页定位方式 webdriver的页面定位很灵活,提供了8种定位方式: 其中,常见的有三种:id .cssSelector .xpath: 一个元素如果存在 id 属性,则这个 id 的值,在 ...

  8. Java Selenium - 几种对话框处理Alert&bsol;confirm&bsol;prompt

    1. Alert , 先用常规办法定位到能触发alert的按钮 , 然后 Alert alert = driver.switchTo().alert(); alert.accept(); 如果aler ...

  9. Selenium&plus;java - 弹出框处理

    一.弹出框分类: 弹出框分为两种,一种基于原生JavaScript写出来的弹窗,另一种是自定义封装好的样式的弹出框,本文重点介绍原生JavaScript写出来的弹窗,另一种弹窗用click()基本就能 ...

随机推荐

  1. VS2013 有效密钥

    今天打开笔记本上的VS2013,发现试用版到期了,就到网上找密钥,找了一些时候找到一个有效序列号,记录如下: BWG7X-J98B3-W34RT-33B3R-JVYW9

  2. INTERSECT交集运算

    INTERSECT交集是由既属于集合A,又属于集合B的所有元素组成的集合,如示意图1.

  3. struts2中constant参数设置

    序号 方法 说明 1 <constant name="struts.i18n.encoding" value="UTF-8"/> 指定web应用默认 ...

  4. javascript 单个图片的淡入淡出效果和多张图片的淡入淡出效果

    最近刚好在看之前妙趣网站的javascript 初级运动教程,教程里说设置图片的透明度使用了一个变量.这种方法确实不错,但是燕姐喜欢麻烦.就用自己的理解方法写了一遍.其中也是各种坑.现在先把一个图片的 ...

  5. 去掉iphone 的圆角样式

    每次面对iphone这种丑丑的样式,我简直不能再愉快的写代码~~而且每次记不住那烦人的属性~~~必须记录下来~~ -webkit-appearance:none 为了下次不用再百度,终于背下来~~~

  6. linux文件系统下的特殊权限

    SUID, SGID, Sticky 1 权限 r, w, x user, group, other 2 安全上下文 前提:进程有属主和属组:文件有属主和属组: (1) 任何一个可执行程序文件能不能启 ...

  7. 解决ubuntu不能安装g&plus;&plus;的问题

    下面提供一种解决方法,解决方法不唯一 首先贴出错误原因: 上文是g++-4.8不能下载,所以退而求其次,指定版本4.7,不下载最新的 解决方法如下: 安装成功后而已查看版本信息确认 使用g++-4.7 ...

  8. Java与算法之&lpar;13&rpar; - 二叉搜索树

    查找是指在一批记录中找出满足指定条件的某一记录的过程,例如在数组{ 8, 4, 12, 2, 6, 10, 14, 1, 3, 5, 7, 9, 11, 13, 15 }中查找数字15,实现代码很简单 ...

  9. android事件处理概括

    什么是事件处理? 事件处理就是针对用户的一些特定操作,进行相对应的回馈.时间处理也是程序开发中的人机交互的一个非常重要的体现.事件处理中,事件源是事件的起始位. 一.事件处理三要素 事件源——事件—— ...

  10. os&period;path&period;md

    os.path 我们可以利用os.path模块提供的函数更容易地在跨平台上处理文件. 即使我们的程序不是用于夸平台, 也应该使用os.path来让路径名字更加可靠. Parsing Paths os. ...