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

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

          導航

          統計

          常用鏈接

          留言簿(55)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 赤壁市| 太和县| 石渠县| 大悟县| 美姑县| 郎溪县| 茂名市| 孝义市| 肥西县| 文登市| 阿克陶县| 青神县| 彰武县| 南木林县| 杭锦后旗| 衡东县| 阿坝| 赤水市| 陈巴尔虎旗| 高安市| 济南市| 宁都县| 上高县| 西藏| 舟山市| 资阳市| 资源县| 高台县| 淅川县| 宁武县| 旬邑县| 东乡县| 乐山市| 句容市| 鄂温| 东明县| 昌吉市| 高邮市| 瓮安县| 上虞市| 平武县|