def select_by_index(self, page_name, obj_name, index): element = self._get_element(page_name, obj_name) if element is not None: select = Select(element) if 0 <= index < len(select.options()): select.select_by_index(index) else: select.select_by_index(0) self.wait(1)
from selenium import webdriver from selenium.webdriver.support.select import Select from time import sleep driver = webdriver.Firefox() driver.get("http://192.168.154.129/ecshop") # 定位列表框 cat = driver.find_element_by_id("category") # 封装列表框类型的对象 cat_select = Select(cat) # type:Select # 操作列表框:通过标号选择第三个选项(从0开始) cat_select.select_by_index(2) #cat_select.deselect_by_index(2) # 取消是能取消多选列表框 sleep(1) # 操作列表框:通过值选择选项,value=12(充值卡)的 cat_select.select_by_value("12") #cat_select.deselect_by_value("12") sleep(1) # 操作列表框:通过显示文本选择选项,充值卡 cat_select.select_by_visible_text("充值卡") #cat_select.deselect_by_visible_text("充值卡") cat_list = cat_select.options() for item in cat_list: print(item.text) sleep(1) driver.quit()
class SelectElement(WebElement): def __init__(self, element_type, locator): WebElement.__init__(self, element_type, locator) self.select_instance = None def bind(self): if self.select_instance == None: self.select_instance = Select(self.get_element()) def get_options(self): self.bind() try: return self.select_instance.options() except: Log.log_error_info("Couldn't get option %s \n") def get_selected_option(self): ''' 获得当前选中的条目 ''' self.bind() try: return self.select_instance.first_selected_option except: Log.log_error_info("Couldn't get selected option %s \n") def selected_option_should_equal(self, option): ''' 比对选中的条目符合特定的选项 ''' selected_option = self.get_selected_option().text.strip() if option != selected_option: Log.log_error_info("selected option is %s, not %s \n" % (selected_option, option)) Log.log_step("下拉框元素当前选中为: %s.(元素位置:%s)" % (option, self.locator)) def select_by_value(self, value): ''' 选中指定的内容项 ''' self.bind() try: self.select_instance.select_by_value(value) except: Log.log_error_info("Couldn't select option %s \n" % (value)) Log.log_step("下拉框元素选中: %s.(元素位置:%s)" % (value, self.locator)) def select_by_index(self, index=1): ''' 选中指定的index项 ''' self.bind() self.select_instance.select_by_index(index) Log.log_step("下拉框元素选中位置为%d的选项.(元素位置:%s)" % (index, self.locator)) def select_by_option(self, option): ''' 选中指定的文本项目 ''' self.bind() self.select_instance.select_by_visible_text(option) Log.log_step("下拉框元素选中: %s.(元素位置:%s)" % (option, self.locator))
# select class # Find id of option x = driver.find_element_by_id('RESULT_RadioButton-9') drop = Select(x) drop.is_multiple() # Select by index drop.select_by_index(2) drop.select_by_visible_text('') drop.select_by_value('') drop.deselect_all() drop.deselect_by_index(0) drop.deselect_by_value('') drop.deselect_by_visible_text('') drop.options() drop.first_selected_option() drop.all_selected_options() # Action class # create action chain object action = ActionChains(driver) menu = driver.find_element_by_css_selector(".nav") hidden_submenu = driver.find_element_by_css_selector(".nav # submenu1") action.click(menu).perform() action.context_click(menu).perform() action.double_click(menu).perform() action.click_and_hold(menu).perform()