本節要解決的問題:
如何定位一組元素?
情境
從上一節的例子中可以看出,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/