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)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 汉阴县| 亳州市| 克什克腾旗| 施秉县| 乌兰县| 玉田县| 商水县| 罗城| 潞城市| 湘乡市| 化德县| 南召县| 襄垣县| 凤凰县| 纳雍县| 六安市| 菏泽市| 辽源市| 朔州市| 达拉特旗| 正镶白旗| 密山市| 澎湖县| 泰州市| 曲周县| 格尔木市| 江油市| 南京市| 宾川县| 潜江市| 九台市| 宜兰县| 郯城县| 娄烦县| 马尔康县| 饶河县| 泌阳县| 垫江县| 读书| 潞城市| 高邮市|