Selenium-webdriver系列教程(7)————如何处理alert和confirm

以前使用watir 1.6x 的时候处理页面javascript弹出的alert和confrim窗口时必须借助autoit工具来辅助执行,就像中国男足职业联赛中高价聘请外援一般。

在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 'rubygems'
require 'selenium-webdriver'

dr = Selenium::WebDriver.for :firefox


frame_file = 'file:///'.concat File.expand_path(File.join(File.dirname(__FILE__), 'alert.html'))


dr.navigate.to frame_file


dr.find_element(:id =>'btn').click


a = dr.switch_to.alert


puts a.text #--> hello


a.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方法。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值