qileilove

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

          輕松自動化---selenium-webdriver(python) (四)

           本節(jié)要解決的問題:

            如何定位一組元素?

            場景

            從上一節(jié)的例子中可以看出,webdriver可以很方便的使用findElement方法來定位某個特定的對象,不過有時候我們卻需要定位一組對象,

            這時候就需要使用findElements方法。

            定位一組對象一般用于以下場景:

            · 批量操作對象,比如將頁面上所有的checkbox都勾上

            · 先獲取一組對象,再在這組對象中過濾出需要具體定位的一些對象。比如定位出頁面上所有的checkbox,然后選擇最后一個

          <html>
          <head>
          <meta http-equiv="content-type" content="text/html;charset=utf-8" />
          <title>Checkbox</title>
          <script type="text/javascript" async="" src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
          <link href=http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/css/bootstrap-combined.min.css rel="stylesheet" />
          <script src="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/js/bootstrap.min.js"></script>
          </head>
          <body>
          <h3>checkbox</h3>
          <div class="well">
          <form class="form-horizontal">
          <div class="control-group">
          <label class="control-label" for="c1">checkbox1</label>
          <div class="controls">
          <input type="checkbox" id="c1" />
          </div>
          </div>
          <div class="control-group">
          <label class="control-label" for="c2">checkbox2</label>
          <div class="controls">
          <input type="checkbox" id="c2" />
          </div>
          </div>
          <div class="control-group">
          <label class="control-label" for="c3">checkbox3</label>
          <div class="controls">
          <input type="checkbox" id="c3" />
          </div>
          </div>

          <div class="control-group">
          <label class="control-label" for="r">radio</label>
          <div class="controls">
          <input type="radio" id="r1" />
          </div>
          </div>

          <div class="control-group">
          <label class="control-label" for="r">radio</label>
          <div class="controls">
          <input type="radio" id="r2" />
          </div>
          </div>
          </form>
          </div>
          </body>
          </html>



           將這段代碼保存復制到記事本中,將保存成checkbox.html文件。(注意,這個頁面需要和我們的自動化腳本放在同一個目錄下)

             check box

            第一種方法:

            通過瀏覽器打個這個頁面我們看到三個復選框和兩個單選框。下面我們就來定位這三個復選框。

          # -*- coding: utf-8 -*-
          from selenium import webdriver
          import time
          import os

          dr = webdriver.Firefox()
          file_path =  'file:///' + os.path.abspath('checkbox.html')
          dr.get(file_path)

          # 選擇頁面上所有的input,然后從中過濾出所有的checkbox并勾選之
          inputs = dr.find_elements_by_tag_name('input')
          for input in inputs:
              if input.get_attribute('type') == 'checkbox':
                  input.click()
          time.sleep(2)

          dr.quit()

            你可以試著把input.get_attribute('type') == 'checkbox' 中的checkbox 變成radio ,那這個腳本定位的會是兩個單選框。

            第二種定位方法:

          # -*- coding: utf-8 -*-
          from selenium import webdriver
          import time
          import os

          dr = webdriver.Firefox()
          file_path =  'file:///' + os.path.abspath('checkbox.html')
          dr.get(file_path)

          # 選擇所有的checkbox并全部勾上
          checkboxes = dr.find_elements_by_css_selector('input[type=checkbox]')
          for checkbox in checkboxes:
              checkbox.click()
          time.sleep(2)

          # 打印當前頁面上有多少個checkbox
          print len(dr.find_elements_by_css_selector('input[type=checkbox]'))
          time.sleep(2)

          dr.quit()

            第二種寫法與第一種寫法差別不大,都是通過一個循環(huán)來勾選控件;如果你學過上一章的話,細心的你一定發(fā)現用的定位函數不一樣,第一種用的name ,第二種用的CSS 。

            如何去掉勾選:

            還有一個問題,有時候我們并不想勾選頁面的所有的復選框(checkbox),可以通過下面辦法把最后一個被勾選的框去掉。如下:

          # -*- coding: utf-8 -*-
          from selenium import webdriver
          import time
          import os

          dr = webdriver.Firefox()
          file_path =  'file:///' + os.path.abspath('checkbox.html')
          dr.get(file_path)

          # 選擇所有的checkbox并全部勾上
          checkboxes = dr.find_elements_by_css_selector('input[type=checkbox]')
          for checkbox in checkboxes:
              checkbox.click()
          time.sleep(2)

          # 把頁面上最后1個checkbox的勾給去掉
          dr.find_elements_by_css_selector('input[type=checkbox]').pop().click()
          time.sleep(2)

          dr.quit()


            其實,去掉勾選表也邏輯也非常簡單,就是再次點擊勾選的按鈕。可能我們比較迷惑的是如何找到“最后一個”按鈕。pop() 可以實現這個功能。

            好吧!在web自動化的學習過程中,我們必須要知道一些前端的東西,這里擴展一下:

            http://www.w3school.com.cn/js/jsref_pop.asp

            嘗試:

            把find_elements_by_css_selector('input[type=checkbox]').pop().click() 中的checkbox 變成radio 會是什么效果,自己嘗試一下吧!

          相關文章:

          輕松自動化---selenium-webdriver(python) (三)

          輕松自動化---selenium-webdriver(python) (五)

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

          <2013年8月>
          28293031123
          45678910
          11121314151617
          18192021222324
          25262728293031
          1234567

          導航

          統(tǒng)計

          常用鏈接

          留言簿(55)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 东阿县| 屯留县| 鄂伦春自治旗| 绥德县| 平塘县| 汨罗市| 图们市| 蓝山县| 田东县| 汪清县| 黑河市| 咸宁市| 牟定县| 康马县| 神木县| 闽侯县| 高邮市| 温州市| 新平| 龙门县| 都匀市| 永城市| 海盐县| 金秀| 盐源县| 尤溪县| 邮箱| 齐河县| 西峡县| 双柏县| 铁力市| 隆昌县| 昌平区| 德清县| 邵东县| 岳阳县| 廉江市| 广元市| 台江县| 阿克陶县| 垦利县|