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


http://www.testclass.net/  測試教程網,專業的selenium 學習網站。

本節要解決的問題:

如何定位一組元素?

 

場景

從上一節的例子中可以看出,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文件。(注意,這個頁面需要和我們的自動化腳本放在同一個目錄下

 

 

 

第一種方法:

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

# -*- 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()

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

第一種用的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 內容:

 「功能測試自動化」匯總


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM