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

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

  1. Dialogs.html
  1. <html>
  2. <head>
  3. <title>Alert</title>
  4. </head>
  5. <body>
  6. <input id = "alert" value = "alert" type = "button" onclick = "alert('欢迎!请按确认继续!');"/>
  7. <input id = "confirm" value = "confirm" type = "button" onclick = "confirm('确定吗?');"/>
  8. <input id = "prompt" value = "prompt" type = "button" onclick = "var name = prompt('请输入你的名字:','请输入
  9. 你的名字'); document.write(name) "/>
  10. </body>
  11. </html>

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

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

  1. import org.openqa.selenium.Alert;
  2. import org.openqa.selenium.By;
  3. import org.openqa.selenium.WebDriver;
  4. import org.openqa.selenium.firefox.FirefoxDriver;
  5. public class DialogsStudy {
  6. /**
  7. * @author gongjf
  8. */
  9. public static void main(String[] args) {
  10. // TODO Auto-generated method stub
  11. System.setProperty("webdriver.firefox.bin","D:\\Program Files\\Mozilla Firefox\\firefox.exe");
  12. WebDriver dr = new FirefoxDriver();
  13. String url = "file:///C:/Documents and Settings/gongjf/桌面/selenium_test/Dialogs.html";// "/Your/Path/to/main.html"
  14. dr.get(url);
  15. //点击第一个按钮,输出对话框上面的文字,然后叉掉
  16. dr.findElement(By.id("alert")).click();
  17. Alert alert = dr.switchTo().alert();
  18. String text = alert.getText();
  19. System.out.println(text);
  20. alert.dismiss();
  21. //点击第二个按钮,输出对话框上面的文字,然后点击确认
  22. dr.findElement(By.id("confirm")).click();
  23. Alert confirm = dr.switchTo().alert();
  24. String text1 = confirm.getText();
  25. System.out.println(text1);
  26. confirm.accept();
  27. //点击第三个按钮,输入你的名字,然后点击确认,最后
  28. dr.findElement(By.id("prompt")).click();
  29. Alert prompt = dr.switchTo().alert();
  30. String text2 = prompt.getText();
  31. System.out.println(text2);
  32. prompt.sendKeys("jarvi");
  33. prompt.accept();
  34. }
  35. }

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

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

selenium webdriver学习(七)------------如何处理alert、confirm、prompt对话框( 转)的更多相关文章

  1. selenium python (十一)alert/confirm/prompt的处理(js中的弹出框)

    webdriver中处理js所生成的alert.confirm以及prompt,采用switch_to_alert()方法定位到alert/confirm/prompt.然后使用text/accept ...

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

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

  3. Python+Selenium学习--alert/confirm/prompt 处理

    场景 webdriver 中处理JavaScript 所生成的alert.confirm 以及prompt 是很简单的.具体思路是使用switch_to.alert()方法定位到alert/confi ...

  4. 转:python webdriver API 之alert/confirm/prompt 处理

    webdriver 中处理 JavaScript 所生成的 alert.confirm 以及 prompt 是很简单的.具体思路是使用switch_to.alert()方法定位到 alert/conf ...

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

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

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

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

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

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

  8. alert/confirm/prompt 处理

    webdriver 中处理JavaScript 所生成的alert.confirm 以及prompt 是很简单的.具体思路是使用switch_to_alert()方法定位到alert/confirm/ ...

  9. 2.11 alert\confirm\prompt

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

随机推荐

  1. ie6 png 透明的解决方法,大网站都在用

    今天解决png图片在IE6下的背景透明问题,找到了一个好方法.之前的解决方案会造成错位,使页面变形.现把这个方法公布如下,本人亲测可以正常使用,如果你在使用中出现问题,请看实例中的说明(英文不精的童鞋 ...

  2. vue 使用 element ui动态添加表单

    html部分 <div class="hello"> <el-form :model="dynamicValidateForm" ref=&q ...

  3. react仿豆瓣

    最近公司在做一个自己内部的图片上传系统,目的是帮助设计人员方便上传图片,用的是vue技术,但是说到vue,可能要提到更早出来的react,react是facebook搞的一套语法糖,也是革命性的用组件 ...

  4. 2019-8-31-dotnet-Framework-源代码-类库的意思

    title author date CreateTime categories dotnet Framework 源代码 类库的意思 lindexi 2019-08-31 16:55:58 +0800 ...

  5. Leetcode637.Average of Levels in Binary Tree二叉树的层平均值

    给定一个非空二叉树, 返回一个由每层节点平均值组成的数组. class Solution { public: vector<double> averageOfLevels(TreeNode ...

  6. Ubuntu上更换163源 - Mars Loo的博客

    转载*请注明原始出处:http://blog.csdn.net/a464057216/article/details/50865895 先备份源/etc/apt/sources.list为source ...

  7. Java IO:为什么InputStream只能读一次

    http://zhangbo-peipei-163-com.iteye.com/blog/2021879 InputStream的接口规范就是这么设计的. /** * Reads the next b ...

  8. 对List<Map<String, Object>>集合排序

    private void mySort(List<Map<String, Object>> list) { //list为待排序的集合,按SEQ字段排序 Comparator& ...

  9. go struct 继承

  10. 集合-Map 接口

    1. 概述   java.util.Map <K,V>接口是一个顶层接口,里面存放的数据单元是:单对元素: K 表示 描述的键 的类型,Key 的类型: V 表示 描述的值 的类型,Val ...