1 def select_from_list(self, locator, *items): 2 """Selects `*items` from list identified by `locator` 3 4 If more than one value is given for a single-selection list, the last 5 value will be selected. If the target list is a multi-selection list, 6 and `*items` is an empty list, all values of the list will be selected. 7 8 *items try to select by value then by label. 9 10 It's faster to use 'by index/value/label' functions. 11 12 An exception is raised for a single-selection list if the last 13 value does not exist in the list and a warning for all other non- 14 existing items. For a multi-selection list, an exception is raised 15 for any and all non-existing values. 16 17 Select list keywords work on both lists and combo boxes. Key attributes for 18 select lists are `id` and `name`. See `introduction` for details about 19 locating elements. 20 """ 21 non_existing_items = [] 22 23 items_str = items and "option(s) '%s'" % ", ".join(items) or "all options" 24 self._info("Selecting %s from list '%s'." % (items_str, locator)) 25 26 select = self._get_select_list(locator) 27 28 if not items: 29 for i in range(len(select.options)): 30 select.select_by_index(i) 31 return 32 33 for item in items: 34 try: 35 select.select_by_value(item) 36 except: 37 try: 38 select.select_by_visible_text(item) 39 except: 40 non_existing_items = non_existing_items + [item] 41 continue 42 43 if any(non_existing_items): 44 if select.is_multiple: 45 raise ValueError("Options '%s' not in list '%s'." % (", ".join(non_existing_items), locator)) 46 else: 47 if any (non_existing_items[:-1]): 48 items_str = non_existing_items[:-1] and "Option(s) '%s'" % ", ".join(non_existing_items[:-1]) 49 self._warn("%s not found within list '%s'." % (items_str, locator)) 50 if items and items[-1] in non_existing_items: 51 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对象