selenium webdriver学习(七)------------如何处理alert、confirm、prompt对话框( 转)
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 webdriver学习(七)------------如何处理alert、confirm、prompt对话框( 转)的更多相关文章
- selenium python (十一)alert/confirm/prompt的处理(js中的弹出框)
webdriver中处理js所生成的alert.confirm以及prompt,采用switch_to_alert()方法定位到alert/confirm/prompt.然后使用text/accept ...
- Java Selenium - 几种对话框处理Alert\confirm\prompt
1. Alert , 先用常规办法定位到能触发alert的按钮 , 然后 Alert alert = driver.switchTo().alert(); alert.accept(); 如果aler ...
- Python+Selenium学习--alert/confirm/prompt 处理
场景 webdriver 中处理JavaScript 所生成的alert.confirm 以及prompt 是很简单的.具体思路是使用switch_to.alert()方法定位到alert/confi ...
- 转:python webdriver API 之alert/confirm/prompt 处理
webdriver 中处理 JavaScript 所生成的 alert.confirm 以及 prompt 是很简单的.具体思路是使用switch_to.alert()方法定位到 alert/conf ...
- Python脚本控制的WebDriver 常用操作 <二十二> 处理alert / confirm / prompt
测试用例场景 webdriver中处理原生的js alert confirm 以及prompt是很简单的.具体思路是使用switch_to.alert()方法定位到alert/confirm/prom ...
- Selenium2学习(十二)-- alert\confirm\prompt
前言 不是所有的弹出框都叫alert,在使用alert方法前,先要识别出到底是不是alert.先认清楚alert长什么样子,下次碰到了,就可以用对应方法解决. alert\confirm\prompt ...
- selenium自动化测试入门 Alert/Confirm/Prompt 弹出窗口处理
一.Alert/Confirm/Prompt弹出窗口特征说明 Alert弹出窗口: 提示用户信息只有确认按钮,无法通过页面元素定位,不关闭窗口无法在页面上做其他操作. Confirm 弹出窗口: 有确 ...
- alert/confirm/prompt 处理
webdriver 中处理JavaScript 所生成的alert.confirm 以及prompt 是很简单的.具体思路是使用switch_to_alert()方法定位到alert/confirm/ ...
- 2.11 alert\confirm\prompt
2.11 alert\confirm\prompt 前言 不是所有的弹出框都叫alert,在使用alert方法前,先要识别出到底是不是alert.先认清楚alert长什么样子,下次碰到了,就可以用 ...
随机推荐
- [jnhs]netbeans使用debug模式频繁出现java.lang.OutOfMemoryError: PermGen space内存不足
netbeans赠送的tomcat7 windows解决方法: 修改C:\Program Files\Apache Software Foundation\Apache Tomcat 8.0.27\b ...
- 删除 BIRT Report Viewer
去掉首页上的标题BIRT Report Viewer方法:找到Webroot\webcontent\birt\pages\layout\FramesetFragment.jsp文件,在里面定义了标题, ...
- new 在C++ 中的用法
我对C++一无所知 看参考手册 来看一下参考手册,总共有三种用法 下面是网站上给出的例子 // operator new example #include <iostream> // st ...
- php中 重载的方法
php中 重载(一)这个文章,谢谢.作为初学者,大牛勿喷: 基本是两个方法 __call,当调用对一个不可访问的对象方法时,会自动执行该魔术方法!(对象调用) 典型的两种处理方式: 1,给出友好的提示 ...
- Django项目:CRM(客户关系管理系统)--13--05PerfectCRM实现King_admin注册功能获取内存02
admin_class.model = modelself.registered_sites[app_name][model_name] = admin_class #注册APP #base_admi ...
- 数据库通过sql备份脚本恢复时,报错误The user specified as a definer ('root'@'%') does not exist
数据库通过sql备份脚本恢复时,报错误The user specified as a definer ('root'@'%') does not exist 当出现这个错误,意思是某个数据库对象的定义 ...
- VMware安装Centos后无法上网
参考于: http://www.xpxt.net/xtjc/win8/04194953.html
- SQL Server删除用户失败的解决方法
在删除SQL Server用户时,有时会报错:Microsoft SQL Server错误: 15138删除对于用户失败,数据库主体在该数据库中拥有架构,无法删除.删除 对于 用户“*****”失败. ...
- 【JZOJ3601】【广州市选2014】Tree(tree)
╰( ̄▽ ̄)╭ 每个非叶子节点,其左右子树叶子节点的权值之和相等.我们称这种二叉树叫平衡二叉树. 我们将一棵平衡二叉树叶子节点的权值从左到右列出来,假如这个权值序列是另一个序列A的子序列,我们称这棵平 ...
- Codeforces Round #275 (Div. 2) A. Counterexample【数论/最大公约数】
A. Counterexample time limit per test 1 second memory limit per test 256 megabytes input standard in ...