您的位置:首页 > 编程语言 > Python开发

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

2013-12-11 22:18 549 查看
下面将使用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()
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: