Selenium webdriver系列教程(6)—如何捕获弹出窗口

web自动化测试中点击一个链接然后弹出新窗口是比较司空见惯的事情。

  webdriver中处理弹出窗口跟处理frame差不多,以下面的html代码为例

window.html
<html>
<head><title>Popup Window</title></head>
<body>
<a id = "soso" href = http://www.soso.com/ target = "_blank">click me</a>
</body>
</html>

  下面的代码演示了如何去捕获弹出窗口

require 'rubygems'
require 'pp'
require 'selenium-webdriver'
dr = Selenium::WebDriver.for :firefox
frame_file = 'file:///'.concat File.expand_path(File.join(File.dirname(__FILE__), 'window.html'))
dr.navigate.to frame_file
dr.find_element(:id =>'soso').click
# 所有的window handles
hs = dr.window_handles
# 当前的window handle
ch = dr.window_handle
pp hs
pp ch
hs.each do |h|
unless h == ch
dr.switch_to.window(h)
p dr.find_element(:id => 's_input')
end
end

  捕获或者说定位弹出窗口的关键在于获得弹出窗口的handle。

  在上面的代码里,使用了windowhandles方法获取所有弹出的浏览器窗口的句柄,然后使用windowhandle方法来获取当前浏览器窗口的句柄,将这两个值的差值就是新弹出窗口的句柄。

  在获取新弹出窗口的句柄后,使用switchto.window(newwindow_handle)方法,将新窗口的句柄作为参数传入既可捕获到新窗口了。

  如果想回到以前的窗口定位元素,那么再调用1次switch_to.window方法,传入之前窗口的句柄既可达到目的。   



最新内容请见作者的GitHub页:http://qaseven.github.io/

   

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值