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

时间:2023-03-09 17:09:19
【selenium】基于python语言,如何用select选择下拉框

在项目测试中遇到了下拉框选择的控件,来总结下如何使用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 <select> elements, not on <%s>" %
webelement.tag_name)
self._el = webelement
multi = self._el.get_attribute("multiple")
self.is_multiple = multi and multi != "false"
 

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: <option value="foo">Bar</option> :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选择下拉框

2、select_by_index
看下代码:
     def select_by_index(self, index):
"""Select the option at the given index. This is done by examing the "index" attribute of an
element, and not merely by counting. :Args:
- index - The option at this index will be selected throws NoSuchElementException If there is no option with specisied index in SELECT
"""
match = str(index)
for opt in self.options:
if opt.get_attribute("index") == match:
self._setSelected(opt)
return
raise NoSuchElementException("Could not locate element with index %d" % index)

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


3、select_by_visible_text
看下代码:
 
     def select_by_visible_text(self, text):
"""Select all options that display text matching the argument. That is, when given "Bar" this
would select an option like: <option value="foo">Bar</option> :Args:
- text - The visible text to match against throws NoSuchElementException If there is no option with specisied text in SELECT
"""
xpath = ".//option[normalize-space(.) = %s]" % self._escapeString(text)
opts = self._el.find_elements(By.XPATH, xpath)
matched = False
for opt in opts:
self._setSelected(opt)
if not self.is_multiple:
return
matched = True if len(opts) == 0 and " " in text:
subStringWithoutSpace = self._get_longest_token(text)
if subStringWithoutSpace == "":
candidates = self.options
else:
xpath = ".//option[contains(.,%s)]" % self._escapeString(subStringWithoutSpace)
candidates = self._el.find_elements(By.XPATH, xpath)
for candidate in candidates:
if text == candidate.text:
self._setSelected(candidate)
if not self.is_multiple:
return
matched = True if not matched:
raise NoSuchElementException("Could not locate element with visible text: %s" % text)
 通过选择文本来匹配,然后给出了例子。看下我的例子:

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

我的代码:

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

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