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 閱讀(452) 評論(0)  編輯  收藏 所屬分類: selenium and watir webdrivers 自動化測試學習

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

          導航

          統計

          常用鏈接

          留言簿(55)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 东明县| 图木舒克市| 惠水县| 宜兰县| 辽中县| 印江| 南木林县| 建昌县| 巧家县| 常宁市| 吴旗县| 山阳县| 满城县| 建昌县| 梨树县| 襄樊市| 永修县| 通化县| 门头沟区| 皮山县| 恩平市| 思南县| 永泰县| 龙江县| 腾冲县| 祁东县| 社会| 鹤山市| 西吉县| 将乐县| 新乐市| 三河市| 冷水江市| 凤庆县| 肃宁县| 石家庄市| 横峰县| 图木舒克市| 饶河县| 娄底市| 海丰县|