【Selenium9】操作按钮

1、处理按钮组button group

  首先找到button group的包裹view,然后层级定位查找具体按钮

用到的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>
代码
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
import os
if 'HTTP_PROXY' in os.environ: del os.environ['HTTP_PROXY']
dr=webdriver.Firefox()
file_path='file:///'+os.path.abspath('buggon_group.html')
dr.get(file_path)
time.sleep(2)
buttons=dr.find_element_by_class_name('btn-group').find_element_by_class_name('btn')
for btn in buttons:
if btn.text=='second': print 'find second button'
sleep(1)
dr.quit()

2、处理button dropdown

  即点击按钮弹出下拉菜单,首先点击按钮,等待下拉菜单显示,然后层级定位获取下拉菜单中的选项

3、处理navs

  即类似tab的导航栏,一般实现是ul+li,先定位ul,再定位li,后通过link解决问题

用到的HTML文档
    <html>
        <head>
            <meta http-equiv="content-type" content="text/html;charset=utf-8" />
            <title>Navs</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(){
                        $('.nav').find('li').click(function() {
                            $(this).parent().find('li').removeClass('active');
                            $(this).addClass('active');
                        });
                    }
                );
            </script>
        </head>
        <body>
            <h3>Navs</h3>
            <div class="row-fluid">
                <div class="span3">     
                    <ul class="nav nav-pills">
                        <li class="active">
                            <a href="#">Home</a>
                        </li>
                        <li><a href="#">Content</a></li>
                        <li><a href="#">About</a></li>
                    </ul>
                </div>      
            </div>      
        </body>
        <script src="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/js/bootstrap.min.js"></script>
    </html>

代码
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
import os
if 'HTTP_PROXY' in os.environ: del os.environ['HTTP_PROXY']
dr=webdriver.Firefox()
file_path='file:///'+os.path.abspath('navs.html')
dr.get(file_path)
time.sleep(2)
dr.find_element_by_class_name('nav').find_element_by_link_text('About').click()  #方法一层级定位
sleep(1)
dr.find_element_by_link_test('Home').click()  #方法二直接定位
sleep(1)
dr.quit()

4、处理面包屑

  即类似站内目录的功能,一般是获取层级关机及当前层级,

用到的HTML文档
    <html>
        <head>
            <meta http-equiv="content-type" content="text/html;charset=utf-8" />
            <title>breadcrumb</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(){

                    }
                );
            </script>
        </head>
        <body>
            <h3>breadcrumb</h3>
            <div class="row-fluid">
                <div class="span3">     
                    <ul class="breadcrumb">
                        <li><a href="#">Home</a> <span class="divider">/</span></li>
                        <li><a href="#">Library</a> <span class="divider">/</span></li>
                        <li class="active">Data</li>
                    </ul>
                </div>      
            </div>      
        </body>
        <script src="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/js/bootstrap.min.js"></script>
    </html>

代码
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
import os
dr=webdriver.Firefox()
file_path='file:///'+os.path.abspath('breadcrumb.html')
dr.get(file_path)
time.sleep(2)
dr.find_element_by_class_name('breadcrumb').find_element_by_link_text('Home').click()  #层级定位
sleep(1)
dr.quit()
posted @ 2014-04-08 06:41  junezhang  阅读(4922)  评论(0编辑  收藏  举报