Selenium-webdriver系列教程(七)————如何处理alert和confirm
2012-06-14 15:23
453 查看
以前使用watir 1.6x 的时候处理页面javascript弹出的alert和confrim窗口时必须借助autoit工具来辅助执行,就像中国男足职业联赛中高价聘请外援一般。
在selenium webdriver中,confirm和alert的处理再也不需要借助任何第三方工具了。
下面的html页面上有1个名为click的button,点击该button后就会弹出1个alert窗口。
selenium webdriver处理alert的代码如下:
上面代码的思路是先点击id为btn的按钮,然后a = dr.switch_to.alert返回了1个alert element(暂时如此理解好了)并赋值给变量a。这样a就代表了alert,使用puts a.text语句可以输出alert的内容,这里会打印出’hello’。
a.accept表示点击确认,当弹出窗口为confrim时,a.accept也表示确认,如果需要取消的话,那么则可以使用a.dismiss方法。
在selenium webdriver中,confirm和alert的处理再也不需要借助任何第三方工具了。
下面的html页面上有1个名为click的button,点击该button后就会弹出1个alert窗口。
<html> <head> <title>Alert</title> </head> <body> <input id = "btn" value = "click" type = "button" onclick = "alert('hello');"/> </body> </html>
selenium webdriver处理alert的代码如下:
require 'selenium-webdriver' dr = Selenium::WebDriver.for :ie alert_file = 'file:///'.concat File.expand_path(File.join(File.dirname(__FILE__),'alert.html')) dr.navigate.to alert_file dr.find_element(:id=>'btn').click alert = dr.switch_to.alert sleep 3 puts alert.text #-->hello alert.accept
上面代码的思路是先点击id为btn的按钮,然后a = dr.switch_to.alert返回了1个alert element(暂时如此理解好了)并赋值给变量a。这样a就代表了alert,使用puts a.text语句可以输出alert的内容,这里会打印出’hello’。
a.accept表示点击确认,当弹出窗口为confrim时,a.accept也表示确认,如果需要取消的话,那么则可以使用a.dismiss方法。
相关文章推荐
- Selenium-webdriver系列教程(七)————如何处理alert和confirm
- Selenium-webdriver系列教程(7)———如何处理alert和confirm
- Selenium-webdriver系列教程(7)————如何处理alert和confirm
- Selenium-webdriver系列教程(13)————如何处理table
- Selenium-webdriver系列教程(三)————如何执行一段js脚本
- Selenium-webdriver系列教程(14)————如何在启动firefox时加载扩展
- Selenium-webdriver系列教程(四)————如何定位测试元素
- Selenium-webdriver系列教程(3)————如何执行一段js脚本
- Selenium-webdriver系列教程(5)———如何定位frame中的元素
- Selenium-webdriver系列教程(10)——如何智能的等待页面加载完成
- Selenium-webdriver系列教程(五)————如何定位frame中的元素
- Selenium-webdriver系列教程(六)————如何捕获弹出窗口
- Selenium-webdriver系列教程(4)————如何定位测试元素
- Selenium-webdriver系列教程(四)————如何定位测试元素
- Selenium-webdriver系列教程(5)————如何定位frame中的元素
- selenium webdriver学习(七)------------如何处理alert、confirm、prompt对话框( 转)
- Selenium-webdriver系列教程(八)————如何操作select下拉框
- Selenium-webdriver系列教程(6)————如何捕获弹出窗口
- Selenium-webdriver系列教程(八)————如何操作select下拉框
- Selenium-webdriver系列教程(九)————如何智能的等待页面加载完成