Pythonスクリプト制御のWebDriver常用操作処理buttonグループ層の位置決め

10716 ワード

Webdriverを使用して同じレイヤのボタンを位置決めします
 

テストケースシーン


buttonグループはボタングループで、ボタンのグループを並べます.
このようなオブジェクトを処理する考え方は、buttonグループのラップdivを見つけてから、階層的に位置決めし、indexまたは属性でより具体的なボタンを位置決めすることです.

Pythonスクリプト


テスト用HTMLコード:
    <html>

        <head>

            <meta http-equiv="content-type" content="text/html;charset=utf-8" />

            <title>button group</title>     

            <script type="text/javascript" async="" src="http://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 type="text/javascript">

                $(document).ready(function(){

                    $('.btn').click(function(){

                        alert($(this).text());

                    });

                });         

            </script>

        </head>

        <body>

            <h3>button group</h3>

            <div class="row-fluid">

                <div class="span3">     

                    <div class="well">

                        <div class="btn-toolbar">

                            <div class="btn-group">

                                <div class="btn">first</div>

                                <div class="btn">second</div>

                                <div class="btn">third</div>

                            </div>

                        </div>

                    </div>          

                </div>      

            </div>      

        </body>

        <script src="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/js/bootstrap.min.js"></script>

    </html>

 
テスト用Pythonコード:
# coding=gbk

'''

Created on 2013 12 11 



@author: Administrator

'''

from selenium import webdriver

from selenium.webdriver.common.keys import Keys

from time import sleep

import os

if 'HTTP_PROXY' in os.environ: del os.environ['HTTP_PROXY']



dr = webdriver.Firefox()

file_path = 'file:///'+ os.path.abspath('button_group.html')

dr.get(file_path)



buttons = dr.find_element_by_class_name('btn-group').find_elements_by_class_name('btn')# "find_elements"

for btn in buttons:# 

    if btn.text == 'second':

        print 'I find the second button' # second 



sleep(5)#5s 

dr.quit()