Python脚本控制的WebDriver 常用操作 <十三> 处理button group层的定位

下面将使用webdriver来定位同一层的按钮

 

测试用例场景


  button group就是按钮组,将一组按钮排列在一起。

  处理这种对象的思路一般是先找到button group的包裹(wrapper)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()

 

转载于:https://www.cnblogs.com/wuzhiming/p/3470210.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值