qileilove

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

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

          相關文章

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

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

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

          導航

          統計

          常用鏈接

          留言簿(55)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 秭归县| 廉江市| 寿光市| 陇川县| 黎川县| 金沙县| 岗巴县| 太康县| 石泉县| 黄大仙区| 东山县| 阜新| 台安县| 尚志市| 万安县| 松江区| 正镶白旗| 濉溪县| 浦东新区| 桃园县| 弥勒县| 海南省| 兖州市| 宁夏| 三穗县| 黔西县| 宝清县| 清远市| 石家庄市| 长武县| 嘉善县| 贺兰县| 蒙阴县| 贡嘎县| 寿阳县| 龙山县| 循化| 咸宁市| 蓝田县| 高台县| 连山|