def select_from_list(self, locator, *items):
"""Selects `*items` from list identified by `locator` If more than one value is given for a single-selection list, the last
value will be selected. If the target list is a multi-selection list,
and `*items` is an empty list, all values of the list will be selected. *items try to select by value then by label. It's faster to use 'by index/value/label' functions. An exception is raised for a single-selection list if the last
value does not exist in the list and a warning for all other non-
existing items. For a multi-selection list, an exception is raised
for any and all non-existing values. Select list keywords work on both lists and combo boxes. Key attributes for
select lists are `id` and `name`. See `introduction` for details about
locating elements.
"""
non_existing_items = [] items_str = items and "option(s) '%s'" % ", ".join(items) or "all options"
self._info("Selecting %s from list '%s'." % (items_str, locator)) select = self._get_select_list(locator) if not items:
for i in range(len(select.options)):
select.select_by_index(i)
return for item in items:
try:
select.select_by_value(item)
except:
try:
select.select_by_visible_text(item)
except:
non_existing_items = non_existing_items + [item]
continue if any(non_existing_items):
if select.is_multiple:
raise ValueError("Options '%s' not in list '%s'." % (", ".join(non_existing_items), locator))
else:
if any (non_existing_items[:-1]):
items_str = non_existing_items[:-1] and "Option(s) '%s'" % ", ".join(non_existing_items[:-1])
self._warn("%s not found within list '%s'." % (items_str, locator))
if items and items[-1] in non_existing_items:
raise ValueError("Option '%s' not in list '%s'." % (items[-1], locator))
方法名:select_from_list(self, locator, *items)
相似方法:
公共方法 选中所传入items,如果给了多个值,并且是single-selection list,则最后一值会被选中;如果是 multi-selection list,并且items为空,则所有options都会被选中
接收参数:locator,*items(labels/values)
26行:使用_get_select_list(self, locator)方法,返回Select对象