欧美三区_成人在线免费观看视频_欧美极品少妇xxxxⅹ免费视频_a级毛片免费播放_鲁一鲁中文字幕久久_亚洲一级特黄

【selenium】基于python語言,如何用select選擇下拉框

系統(tǒng) 1901 0

在項(xiàng)目測試中遇到了下拉框選擇的控件,來總結(jié)下如何使用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)

          

?就是說使用這個方法,下拉框?qū)傩孕枰衯alue,如果選項(xiàng)中不具有指定值的項(xiàng),就拋異常。例如:

          
            ?
            
              【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)
          

?

?

          
            ?
          
        
          
            
              
                
                  
                    
                      
                        
                          
                            
                              
                                
                                  
                                    
                                      
                                        
                                          
                                            
                                              
                                                
                                                  
                                                    
                                                      
                                                        ?
                                                      
                                                    
                                                  
                                                
                                              
                                            
                                          
                                        
                                      
                                    
                                  
                                
                              
                            
                          
                        
                      
                    
                  
                
              
            
          
        

?


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 特黄做愛又硬又大A片视频 小视频在线看 | 久久久人 | 午夜在线 | 色综合久久88中文字幕 | 国产成人lu在线视频 | 国产欧美日韩精品a在线观看高清 | 麻豆网站入口 | 成人免费视频网站在线观看 | 成人全黄三级视频在线观看 | 久久乐国产精品 | 午夜不卡电影 | 欧美a区| 国产精品久久久久久免费 | 日韩精品视频一区二区三区 | 国产亚洲一区在线 | 99在线播放视频 | 欧美激情一区二区三区视频高清 | 一级特黄视频 | 搞黄网站免费观看 | 欧美日韩综合精品一区二区三区 | 一个人看aaaa免费中文 | 国产精品视频999 | 免费看91| 久久久久综合精品福利啪啪 | 一区二区三区四区在线观看视频 | 91久久国产精品 | 青青免费视频精品一区二区 | 日韩高清在线亚洲专区vr | 亚洲一区国产视频 | 99久久99久久精品免费看蜜桃 | 中文字幕在线一区 | 一级电影免费 | 黄色影片在线免费观看 | www.精品久久 | 亚洲激情 欧美 | 中文字幕三区 | 久草视频网站 | 6全高清智能录播系统视频 精品九九 | 国产午夜精品一区二区三区嫩草 | 欧美另类色 | 天天拍天天干天天操 |