qileilove

          blog已經轉移至github,大家請訪問 http://qaseven.github.io/

          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方法,傳入之前窗口的句柄既可達到目的。

          相關文章

          Selenium webdriver系列教程(5)—如何定位frame中的元素

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

          posted on 2013-09-23 10:50 順其自然EVO 閱讀(460) 評論(0)  編輯  收藏 所屬分類: selenium and watir webdrivers 自動化測試學習

          <2013年9月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          293012345

          導航

          統計

          常用鏈接

          留言簿(55)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 泾川县| 华阴市| 景东| 崇义县| 鲁甸县| 乌兰察布市| 北宁市| 柳林县| 屯昌县| 巴彦县| 仁寿县| 江山市| 株洲县| SHOW| 滨州市| 苍梧县| 朝阳区| 原平市| 张家港市| 栾城县| 武安市| 彰武县| 吉木萨尔县| 潼关县| 百色市| 淅川县| 德清县| 庄浪县| 康定县| 福建省| 湟源县| 扶风县| 临高县| 黔东| 东方市| 眉山市| 两当县| 双辽市| 离岛区| 鄂托克旗| 五华县|