码迷,mamicode.com
首页 > 其他好文 > 详细

15.处理button group

时间:2018-06-11 18:00:37      阅读:137      评论:0      收藏:0      [点我收藏+]

标签:区别   col   EAP   func   round   env   elements   按钮组   first   

场景

button group就是按钮组,将一组按钮排列在一起。处理这种对象的思路一般是先找到button group的包裹(wrapper)div,然后通过层级定位,用index或属性去定位更具体的按钮。

下面的代码演示了如何找到second这个按钮。其处理方法是先找到button group的父div,class为btn-group的div,然后再找到下面所有的div(也就是button),返回text是second的div。

button_group.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>

注:button_group.html和test.py放在同一个文件夹下

 

创建test.py输入一下代码

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.Chrome()
file_path = file:/// + os.path.abspath(button_group.html)
dr.get(file_path)
sleep(1)

# 定位text是second的按钮
buttons = dr.find_element_by_class_name(btn-group).find_elements_by_class_name(btn)
for btn in buttons:
    if btn.text == second : print (find second button)
    
sleep(1)
dr.quit()

 

拓展:

element和elements的区别

https://www.cnblogs.com/yoyoketang/p/6557421.html

15.处理button group

标签:区别   col   EAP   func   round   env   elements   按钮组   first   

原文地址:https://www.cnblogs.com/luoshuifusheng/p/9168236.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!