alert、confirm、prompt这样的js对话框在selenium1.X时代也是难啃的骨头,常常要用autoit来帮助处理。

试用了一下selenium webdriver中处理这些对话框十分方便简洁。以下面html代码为例:

Dialogs.html

<html>  

    <head>  

        <title>Alert</title>  

    </head>  

    <body>  

        <input id = "alert" value = "alert" type = "button" onclick = "alert('欢迎!请按确认继续!');"/>
<input id = "confirm" value = "confirm" type = "button" onclick = "confirm('确定吗?');"/>
<input id = "prompt" value = "prompt" type = "button" onclick = "var name = prompt('请输入你的名字:','请输入 你的名字'); document.write(name) "/> </body> </html>

以上html代码在页面上显示了三个按钮,点击他们分别弹出alert、confirm、prompt对话框。如果在prompt对话框中输入文字点击确定之后,将会刷新页面,显示出这些文字 。

selenium webdriver 处理这些弹层的代码如下:

import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver; public class DialogsStudy { /**
* @author gongjf
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.setProperty("webdriver.firefox.bin","D:\\Program Files\\Mozilla Firefox\\firefox.exe");
WebDriver dr = new FirefoxDriver();
String url = "file:///C:/Documents and Settings/gongjf/桌面/selenium_test/Dialogs.html";// "/Your/Path/to/main.html"
dr.get(url); //点击第一个按钮,输出对话框上面的文字,然后叉掉
dr.findElement(By.id("alert")).click();
Alert alert = dr.switchTo().alert();
String text = alert.getText();
System.out.println(text);
alert.dismiss(); //点击第二个按钮,输出对话框上面的文字,然后点击确认
dr.findElement(By.id("confirm")).click();
Alert confirm = dr.switchTo().alert();
String text1 = confirm.getText();
System.out.println(text1);
confirm.accept(); //点击第三个按钮,输入你的名字,然后点击确认,最后
dr.findElement(By.id("prompt")).click();
Alert prompt = dr.switchTo().alert();
String text2 = prompt.getText();
System.out.println(text2);
prompt.sendKeys("jarvi");
prompt.accept(); } }

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

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

Selenium常用操作汇总二——如何处理alert、confirm、prompt对话框的更多相关文章

  1. Selenium常用操作汇总二——如何操作select下拉框

    下面我们来看一下selenium webdriver是如何来处理select下拉框的,以http://passport.51.com/reg2.5p这个页面为例.这个页面中有4个下拉框,下面演示4种选 ...

  2. Selenium常用操作汇总二——如何操作cookies(转)

    Web 测试中我们经常会接触到Cookies,一个Cookies主要属性有”所在域.name.value.有效日期和路径",下面来讲一下怎么操作Cookies. import java.ut ...

  3. Selenium常用操作汇总二——如何把一个元素拖放到另一个元素里面(转)

    Q群里有时候会有人问,selenium  webdriver怎么实现把一个元素拖放到另一个元素里面.这一节总一下元素的拖放. 下面这个页面是一个演示拖放元素的页面,你可以把左右页面中的条目拖放到右边的 ...

  4. Selenium常用操作汇总二——如何得到弹出窗口

    在selenium 1.X里面得到弹出窗口是一件比较麻烦的事,特别是新开窗口没有id.name的时候.当时还整理了处理了几种方法,详见:http://seleniumcn.cn/read.php?ti ...

  5. Selenium常用操作汇总二——iframe的处理

    有时候我们在定位一个页面元素的时候发现一直定位不了,反复检查自己写的定位器没有任何问题,代码也没有任何问题.这时你就要看一下这个页面元素是否在一个iframe中,这可能就是找不到的原因之一.如果你在一 ...

  6. Python脚本控制的WebDriver 常用操作 <二十二> 处理alert / confirm / prompt

    测试用例场景 webdriver中处理原生的js alert confirm 以及prompt是很简单的.具体思路是使用switch_to.alert()方法定位到alert/confirm/prom ...

  7. Selenium2学习(十二)-- alert\confirm\prompt

    前言 不是所有的弹出框都叫alert,在使用alert方法前,先要识别出到底是不是alert.先认清楚alert长什么样子,下次碰到了,就可以用对应方法解决. alert\confirm\prompt ...

  8. selenium自动化测试入门 Alert/Confirm/Prompt 弹出窗口处理

    一.Alert/Confirm/Prompt弹出窗口特征说明 Alert弹出窗口: 提示用户信息只有确认按钮,无法通过页面元素定位,不关闭窗口无法在页面上做其他操作. Confirm 弹出窗口: 有确 ...

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

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

随机推荐

  1. Vivado与SDK的联合调试方法-使用ILA

    首先介绍一下我的硬件平台:使用的开发板为米联客出的MIZ702,这个开发板与ZedBoard是兼容的. Vivado硬件调试有几种手段:ILA(集成逻辑分析器Integrated Logic Anal ...

  2. Spring下面的classpath 和 classpath* 区别的简单讲解

    classpath 和 classpath* 区别: classpath:只会到你指定的class路径中查找找文件; classpath*:不仅包含class路径,还包括jar文件中(class路径) ...

  3. javascript基础拾遗(七)

    1.对象的继承__proto__ var Language = { name: 'program', score: 8.0, popular: function () { return this.sc ...

  4. 关于RPG游戏结构撰写的相关探索上篇

    本章节的目标是创造一个游戏理念.这个理念是: *简短的项目概括 *范围描述 *目标用户 *与其他游戏的区别 不要试图编写一款缺乏明确理念的RPG.因为这样可能只会产生与其他游戏雷同的项目. <i ...

  5. Delphi下IOCP开源框架:DIOCP 成功应用案例分享

    首先说明,该项目不是本人的项目,本文转自盒子. 该项目使用的DIOCP版本为1.0,目前diocp为3.5 以下是盒子的原文 ------------------------------------- ...

  6. 2015-2016款Mac安装win10多分区教程,不破坏GUID分区表。

    原文:https://bbs.feng.com/read-htm-tid-10895240.html 参考:https://bbs.feng.com/read-htm-tid-9940193.html ...

  7. python rabittmq 使用

    Reference: https://www.rabbitmq.com/tutorials/tutorial-three-python.html 1 "Hello World!" ...

  8. 无刷新文件上传 利用iframe实现

    效果如图: 示例是基于asp.net mvc实现的 html代码: <form id="form2" name="form2" class="f ...

  9. 基于9款CSS3鼠标悬停相册预览特效

    基于9款CSS3鼠标悬停相册预览特效里面包含九款不同方式的相册展开特效代码.效果图如下: 在线预览   源码下载 实现的代码. html代码: <div class="albums&q ...

  10. [转]获取JAVA[WEB]项目相关路径的几种方法

    http://blog.csdn.net/yaerfeng/article/details/7297479/ 在jsp和class文件中调用的相对路径不同. 在jsp里,根目录是WebRoot 在cl ...