12.定位一组对象

场景

从上一节的例子中可以看出,webdriver可以很方便的使用find_element方法来定位某个特定的对象,不过有时候我们却需要定位一组对象,这时候就需要使用find_elements方法。

定位一组对象一般用于以下场景:

  • 批量操作对象,比如将页面上所有的checkbox都勾上
  • 先获取一组对象,再在这组对象中过滤出需要具体定位的一些对象。比如定位出页面上所有的checkbox,然后选择最后一个

创建checkbox.html

<html>
        <head>
            <meta http-equiv="content-type" content="text/html;charset=utf-8" />
            <title>Checkbox</title>
            <script type="text/javascript" async="" src="https://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 src="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/js/bootstrap.min.js"></script>
        </head>
        <body>
            <h3>checkbox</h3>
            <div class="well">
                <form class="form-horizontal">
                    <div class="control-group">
                        <label class="control-label" for="c1">checkbox1</label>
                        <div class="controls">
                            <input type="checkbox" id="c1" />
                        </div>
                    </div>
                    <div class="control-group">
                        <label class="control-label" for="c2">checkbox2</label>
                        <div class="controls">
                            <input type="checkbox" id="c2" />
                        </div>
                    </div>
                    <div class="control-group">
                        <label class="control-label" for="c3">checkbox3</label>
                        <div class="controls">
                            <input type="checkbox" id="c3" />
                        </div>
                    </div>                        
                    <div class="control-group">
                        <label class="control-label" for="r">radio</label>
                        <div class="controls">
                            <input type="radio" id="r" />
                        </div>
                    </div>                        
                </form>
            </div>
        </body>
    </html>

创建test.py写入以下代码

猜你喜欢

转载自www.cnblogs.com/luoshuifusheng/p/9151206.html