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

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

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

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

一、操作 Alert 弹框


alert在网页的HTML代码:

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

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


  1. package com.automation.alert;
  2. import org.openqa.selenium.Alert;
  3. import org.openqa.selenium.By;
  4. import org.openqa.selenium.NoAlertPresentException;
  5. import org.openqa.selenium.WebDriver;
  6. import org.openqa.selenium.WebElement;
  7. import org.openqa.selenium.chrome.ChromeDriver;
  8. /**
  9. * 类说明:操作alert弹框
  10. * <br/>
  11. * @version 1.0
  12. * 2016年11月19日 下午9:47:12
  13. */
  14. public class AlertDemo {
  15. private static WebDriver driver = null ;
  16. private static String chromeDriverDir = "D:\\workspace\\A_Test\\resource\\chromedriver.exe";
  17. public static void main(String[] args) {
  18. //1.打开浏览器;
  19. System.setProperty("webdriver.chrome.driver", chromeDriverDir);
  20. driver = new ChromeDriver();
  21. driver.manage().window().maximize();
  22. //打开文件网址;
  23. driver.get("file:///E:/desktop/upload.html");
  24. //定位alert按钮对象;
  25. WebElement alertButton = driver.findElement(By.id("alert"));
  26. //点击alert按钮,弹出alert弹出框
  27. alertButton.click();
  28. try {
  29. //获取Alert弹框对象;
  30. Alert alertWindow = driver.switchTo().alert();
  31. //获取alert弹框的文本,并打印到控制台;
  32. String alertText = alertWindow.getText();
  33. System.out.println(alertText);
  34. //点击alert弹出框中的确定按钮;
  35. alertWindow.accept();
  36. } catch (NoAlertPresentException e) {
  37. System.out.println("尝试操作的alert弹出框未找到!");
  38. e.printStackTrace();
  39. }
  40. }
  41. }

二、操作confirm弹框


confirm在网页的HTML代码:

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

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


  1. package com.automation.alert;
  2. import org.openqa.selenium.Alert;
  3. import org.openqa.selenium.By;
  4. import org.openqa.selenium.NoAlertPresentException;
  5. import org.openqa.selenium.WebDriver;
  6. import org.openqa.selenium.WebElement;
  7. import org.openqa.selenium.chrome.ChromeDriver;
  8. /**
  9. * 类说明:操作confirm弹框
  10. * <br/>
  11. * @version 1.0
  12. * 2016年11月19日 下午9:46:47
  13. */
  14. public class ConfirmDemo {
  15. private static WebDriver driver = null ;
  16. private static String chromeDriverDir = "D:\\workspace\\A_Test\\resource\\chromedriver.exe";
  17. public static void main(String[] args) {
  18. //1.打开浏览器;
  19. System.setProperty("webdriver.chrome.driver", chromeDriverDir);
  20. driver = new ChromeDriver();
  21. driver.manage().window().maximize();
  22. //打开文件网址;
  23. driver.get("file:///E:/desktop/upload.html");
  24. //定位confirm按钮对象;
  25. WebElement confirmButton = driver.findElement(By.id("confirm"));
  26. //点击confirm按钮,弹出confirm弹出框
  27. confirmButton.click();
  28. //获取Alert弹框对象;
  29. Alert confirmWindow = null;
  30. try {
  31. confirmWindow = driver.switchTo().alert();
  32. //获取confirm弹框的文本,并打印到控制台;
  33. String confirmText = confirmWindow.getText();
  34. System.out.println(confirmText);
  35. //点击confirm弹出框中的确定按钮;
  36. confirmWindow.accept();
  37. //重新操作,点击confirm弹框的取消按钮;
  38. confirmButton.click();
  39. confirmWindow = driver.switchTo().alert();
  40. confirmWindow.dismiss();
  41. } catch (NoAlertPresentException e) {
  42. System.out.println("尝试操作的confirm弹出框未找到!");
  43. e.printStackTrace();
  44. }
  45. }
  46. }

三、操作prompt弹框


prompt在网页的HTML代码:

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

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

QQ图片20161119215628.png705x240 12.7 KB

  1. package com.automation.alert;
  2. import org.openqa.selenium.Alert;
  3. import org.openqa.selenium.By;
  4. import org.openqa.selenium.NoAlertPresentException;
  5. import org.openqa.selenium.WebDriver;
  6. import org.openqa.selenium.WebElement;
  7. import org.openqa.selenium.chrome.ChromeDriver;
  8. /**
  9. * 类说明:操作prompt弹框
  10. * <br/>
  11. * @version 1.0
  12. * 2016年11月19日 下午10:02:42
  13. */
  14. public class PromptDemo {
  15. private static WebDriver driver = null ;
  16. private static String chromeDriverDir = "D:\\workspace\\A_Test\\resource\\chromedriver.exe";
  17. public static void main(String[] args) {
  18. //1.打开浏览器;
  19. System.setProperty("webdriver.chrome.driver", chromeDriverDir);
  20. driver = new ChromeDriver();
  21. driver.manage().window().maximize();
  22. //打开文件网址;
  23. driver.get("file:///E:/desktop/upload.html");
  24. //定位prompt按钮对象;
  25. WebElement promptButton = driver.findElement(By.id("prompt"));
  26. //点击prompt按钮,弹出prompt弹出框
  27. promptButton.click();
  28. //获取Alert弹框对象;
  29. Alert promptWindow = null;
  30. try {
  31. promptWindow = driver.switchTo().alert();
  32. //获取prompt弹框的文本,并打印到控制台;
  33. String confirmText = promptWindow.getText();
  34. System.out.println(confirmText);
  35. //向prompt弹框中的输入框对象,输入文本;
  36. promptWindow.sendKeys("selenium + webdriver!");
  37. //点击prompt弹出框中的确定按钮;
  38. promptWindow.accept();
  39. //重新操作,点击prompt弹框的取消按钮;
  40. promptButton.click();
  41. promptWindow = driver.switchTo().alert();
  42. promptWindow.dismiss();
  43. } catch (NoAlertPresentException e) {
  44. System.out.println("尝试操作的prompt弹出框未找到!");
  45. e.printStackTrace();
  46. }
  47. }
  48. }

从以上代码可以看出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=" ...

  2. 基于Selenium2+Java的UI自动化(4) - WebDriver API简单介绍

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

  3. 基于Selenium2+Java的UI自动化(1) - 原理和环境搭建

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

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

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

  5. 基于Selenium2+Java的UI自动化(5) - 执行JavaScript脚本

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

  6. 基于Selenium2+Java的UI自动化(2) - 启动浏览器

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

  7. 基于Selenium2+Java的UI自动化(3) - 页面元素定位

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

  8. Java Selenium - 几种对话框处理Alert\confirm\prompt

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

  9. Selenium+java - 弹出框处理

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

随机推荐

  1. 13、手把手教你Extjs5(十三)模块字段和Grid列的定义[1]

    这一节加入模块自定义字段,并根据这些字段生成model.然后再定义grid中的分组和列.从这一切开始真正进入到了模块自定义的节奏当中,代码的复杂度和技巧性也大大提高.先从模块字段的自定义开始.先看一下 ...

  2. js div截取字符串的长度

    <div style="width:100%;" id="changdu">这个是字符串的长度</div> $("#chang ...

  3. Java的内存泄漏

    内存泄漏是指,无用对象(不再使用的对象)持续占用内存或者无用对象的内存得不到及时释放,从而造成的内存浪费 就说是有一块内存你不需要再用了,但是呢你还保留着它的指针,那么这块内存就不会被回收 举个例子 ...

  4. 字符串处理,NSNumber转换

    more:http://www.superqq.com/blog/categories/ioskai-fa/ 1.判断字符串是否为空 if ([text lenght] == 0) {     //  ...

  5. javascript-函数声明和函数表达式-call-apply

    1.函数声明与函数表达式 <script type="text/javascript"> //函数表达式,解析器在像执行环境中加载数据时,函数表达式是解析器执行到这段代 ...

  6. GP项目总结(一)

    1.使用activity渲染不同的View时,两种方法: (1.)自定义两个不同的View,然后在mainActivity里根据不同的数据使用不同的View,通过addView()来Activity里 ...

  7. 【Xilinx-Petalinux学习】-02-建立PetaLinux工程

    前面我已经把PetaLinux成功安装到了Ubuntu虚拟机当中了,接下来就要实际操作,将PetaLinux移植到我们自己的硬件平台当中去. step1:硬件描述文件 有两种PetaLinux工程建立 ...

  8. 【腾讯优测干货分享】微信小程序之自动化亲密接触

    本文来自于腾讯优测公众号(wxutest),未经作者同意,请勿转载,原文地址:http://mp.weixin.qq.com/s/HcPakz5CV1SHnu-U8n85pw 导语 山雨欲来风满楼,最 ...

  9. mysql ++中文乱码问题

    使用mysql++读取mysql数据库,数据表中字符集为utf8,但是读取的时候中文字符串不能够正常显示.下面是测试程序: #include <iostream> #include < ...

  10. 2015年15+最佳的响应式HTML5网站模板

    015年最好的免费响应式HTML5模板通常用于创建新潮的网站. HTML5是HTML用于创建现代化网站的最新版本.随着这一现代标记语言的出现,网上冲浪的趋势变得越来越智能化越来越酷.几乎每个web开发 ...