Selenium-webdriver系列教程(八)————如何操作select下拉框


在selenium-webdriver中定位select list的方法比較簡單,用id和name等屬性可以很方便的將select給找出來,但是怎么去選擇下拉框中的某一項呢?

思路是這樣的,首先定位到select list元素,然后找出該select list下所有的option,點擊該option element既可,以下面的html代碼為例

 

<html>
     <head>
         <title>Select</title>
     </head>
     <body>
         <span>select demo</span>
         <select id = "s" name = "ns" >
             <option value = "0" >Op1</option>
             <option value = "1" >Op2</option>
             <option value = "2" >Op3</option>
             <option value = "3" >Op4</option>
         </select>
     </body>
</html>
 
通過下面的代碼可以選擇到下拉框中的第2個option,也就是text為Op2的選項
 

require 'selenium-webdriver'
  
dr = Selenium::WebDriver. for :firefox
 
select_file = 'file:///' .concat File .expand_path( File .join( File .dirname( __FILE__ ), 'select.html' ))
 
dr.navigate.to select_file
 
dr.find_element( :id => 's' ).find_elements( :tag_name => 'option' )[ 1 ].click
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM