【selenium】基于python语言,如何用select选择下拉框

系统 1747 0

在项目测试中遇到了下拉框选择的控件,来总结下如何使用select选择下拉框:

  下图是Select类的初始化描述,意思是,给定元素是得是select类型,不是就抛异常。接下来给了例子:要操作这个select,先要定位到,然后再通过 select_by_index 选择下拉框

          
            
              def 
              
                __init__(
                
                  self
                  
                    , webelement):
                    
"""
Constructor. A check is made that the given element is, indeed, a SELECT tag. If it is not,
then an UnexpectedTagNameException is thrown.

:Args:
- webelement - element SELECT element to wrap

Example:
from selenium.webdriver.support.ui import Select \n
Select(driver.find_element_by_tag_name("select")).select_by_index(2)
"""
if webelement.tag_name.lower() != "select":
raise UnexpectedTagNameException(
"Select only works on
            
               
            
          

1、select_by_value:

          
            看下代码:
            
                def select_by_value(self, value):
        
            
              """Select all options that have a value matching the argument. That is, when given "foo" this
           would select an option like:

           Bar

           :Args:
            - value - The value to match against

           throws NoSuchElementException If there is no option with specisied value in SELECT
           """
            
            
        css = "option[value =%s]" % self._escapeString(value)
        opts = self._el.find_elements(By.CSS_SELECTOR, css)
        matched = False
        for opt in opts:
            self._setSelected(opt)
            if not self.is_multiple:
                return
            matched = True
        if not matched:
            raise NoSuchElementException("Cannot locate option with value: %s" % value)

          

 就是说使用这个方法,下拉框属性需要有value,如果选项中不具有指定值的项,就抛异常。例如:

          
             
            
              【selenium】基于python语言,如何用select选择下拉框_第1张图片
            
            
        
          
            2、select_by_index
            
看下代码:
              
                 1
              
              
                def
              
              
                 select_by_index(self, index):

              
              
                 2
              
              
                """
              
              
                Select the option at the given index. This is done by examing the "index" attribute of an

              
              
                 3
              
              
                           element, and not merely by counting.

              
              
                 4
              
              
                 5
              
              
                           :Args:

              
              
                 6
              
              
                            - index - The option at this index will be selected

              
              
                 7
              
              
                 8
              
              
                           throws NoSuchElementException If there is no option with specisied index in SELECT

              
              
                 9
              
              
                """
              
              
                10
              
                       match =
              
                 str(index)

              
              
                11
              
              
                for
              
               opt 
              
                in
              
              
                 self.options:

              
              
                12
              
              
                if
              
               opt.get_attribute(
              
                "
              
              
                index
              
              
                "
              
              ) ==
              
                 match:

              
              
                13
              
              
                                self._setSelected(opt)

              
              
                14
              
              
                return
              
              
                15
              
              
                raise
              
               NoSuchElementException(
              
                "
              
              
                Could not locate element with index %d
              
              
                "
              
               % index)
            

 

 这个是通过元素的“index”属性来完成

          
            

3、select_by_visible_text
          
            看下代码:
          
        
          
             
          
        
            
               1
            
            
              def
            
            
               select_by_visible_text(self, text):

            
            
               2
            
            
              """
            
            
              Select all options that display text matching the argument. That is, when given "Bar" this

            
            
               3
            
            
                         would select an option like:

            
            
               4
            
            
               5
            
            
                          Bar

            
            
               6
            
            
               7
            
            
                         :Args:

            
            
               8
            
            
                          - text - The visible text to match against

            
            
               9
            
            
              10
            
            
                          throws NoSuchElementException If there is no option with specisied text in SELECT

            
            
              11
            
            
              """
            
            
              12
            
                     xpath = 
            
              "
            
            
              .//option[normalize-space(.) = %s]
            
            
              "
            
             %
            
               self._escapeString(text)

            
            
              13
            
                     opts =
            
               self._el.find_elements(By.XPATH, xpath)

            
            
              14
            
                     matched =
            
               False

            
            
              15
            
            
              for
            
             opt 
            
              in
            
            
               opts:

            
            
              16
            
            
                          self._setSelected(opt)

            
            
              17
            
            
              if
            
            
              not
            
            
               self.is_multiple:

            
            
              18
            
            
              return
            
            
              19
            
                         matched =
            
               True

            
            
              20
            
            
              21
            
            
              if
            
             len(opts) == 0 
            
              and
            
            
              "
            
            
              "
            
            
              in
            
            
               text:

            
            
              22
            
                         subStringWithoutSpace =
            
               self._get_longest_token(text)

            
            
              23
            
            
              if
            
             subStringWithoutSpace == 
            
              ""
            
            
              :

            
            
              24
            
                             candidates =
            
               self.options

            
            
              25
            
            
              else
            
            
              :

            
            
              26
            
                             xpath = 
            
              "
            
            
              .//option[contains(.,%s)]
            
            
              "
            
             %
            
               self._escapeString(subStringWithoutSpace)

            
            
              27
            
                             candidates =
            
               self._el.find_elements(By.XPATH, xpath)

            
            
              28
            
            
              for
            
             candidate 
            
              in
            
            
               candidates:

            
            
              29
            
            
              if
            
             text ==
            
               candidate.text:

            
            
              30
            
            
                                  self._setSelected(candidate)

            
            
              31
            
            
              if
            
            
              not
            
            
               self.is_multiple:

            
            
              32
            
            
              return
            
            
              33
            
                                 matched =
            
               True

            
            
              34
            
            
              35
            
            
              if
            
            
              not
            
            
               matched:

            
            
              36
            
            
              raise
            
             NoSuchElementException(
            
              "
            
            
              Could not locate element with visible text: %s
            
            
              "
            
             % text)
          

 

          
             通过选择文本来匹配,然后给出了例子。看下我的例子:
            

【selenium】基于python语言,如何用select选择下拉框_第2张图片

 我的代码:

 

            
              1
            
                stafftype_loc = (By.XPATH, 
            
              "
            
            
              //select[@ng-model='Invite.type']
            
            
              "
            
            
              )

            
            
              2
            
            
              3
            
                  find_element(*self.stafftype_loc).send_keys(stftype)
          

 

 

          
             
          
        
          
            
              
                
                  
                    
                      
                        
                          
                            
                              
                                
                                  
                                    
                                      
                                        
                                          
                                            
                                              
                                                
                                                  
                                                    
                                                      
                                                         
                                                      
                                                    
                                                  
                                                
                                              
                                            
                                          
                                        
                                      
                                    
                                  
                                
                              
                            
                          
                        
                      
                    
                  
                
              
            
          
        

 


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论