selenium-webdriver(python) (四)

來源:互聯網
上載者:User

本節要解決的問題:

如何定位一組元素?

情境

從上一節的例子中可以看出,webdriver可以很方便的使用findElement方法來定位某個特定的對象,不過有時候我們卻需要定位一組對象,

這時候就需要使用findElements方法。

定位一組對象一般用於以下情境:

大量操作對象,比如將頁面上所有的checkbox都勾上

先擷取一組對象,再在這組對象中過濾出需要具體定位的一些對象。比如定位出頁面上所有的checkbox,然後選擇最後一個

<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="r1" />                    </div>                </div>                                <div class="control-group">                    <label class="control-label" for="r">radio</label>                    <div class="controls">                        <input type="radio" id="r2" />                    </div>                </div>            </form>        </div>    </body></html>

將這段代碼儲存複製到記事本中,將儲存成checkbox.html檔案。(注意,這個頁面需要和我們的自動化指令碼放在同一個目錄下)

查看本欄目更多精彩內容:http://www.bianceng.cnhttp://www.bianceng.cn/Programming/extra/

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.