Select是網頁表單中較爲常見的元素,在Selenium2Library 中也有相應關鍵字能夠操做,好比:ide
(1)經過指定索引選擇函數
Name: Select From List By Index Arguments: [ locator | *indexes ] spa
Selects `*indexes` from list identified by `locator`blog
(2)經過指定文本選擇索引
Name: Select From List By Label Arguments: [ locator | *labels ]get
Selects `*labels` from list identified by `locator`it
(3)經過指定value選擇io
Name: Select From List By Value Arguments: [ locator | *values ]表單
Selects `*values` from list identified by `locator`List
(4)能夠同時選擇多項
Name: Select From List Arguments: [ locator | *items ]
Selects `*items` from list identified by `locator`
(5)選擇全部項
Name: Select All From List Arguments: [ locator ]
Selects all values from multi-select list identified by `id`.
爲了快速操做方便,咱們實現一個讓Select自動賦最大索引值的方法。爲何是最大索引值而不是最小呢,好比第一個option爲"" 或者"請選擇",即便選中也是不符合要求的。因此繼續看代碼:
第1行:get_list_items獲取select 的options集合
第2行:獲取options集合中項的個數,注意此處用'$' 而不是'@'
第3行:經過計算獲得所要的索引值
第4行:借用Select From List By Index 根據索引選擇
至此,已經實現了想要的功能,可是還比較粗糙。若是options 的長度爲0(這種狀況是有可能的) ,而且在實際使用過程當中發現若是options集合過大會有明顯的等待過程,咱們來看下get_list_items函數:
能夠看到函數內部引用了_get_select_list_options 和 _get_labels_for_options
先看_get_select_list_options
這裏只是一個簡單地判斷,並返回結果。看來癥結應該是在_get_labels_for_options函數了
一個for 循環,雖然時間複雜度只有O(n)但也會由於options的長度變長而耗時