def deselect_g(context, selector, text=None, *argv, index=None, value=None, **kw): germanium = find_germanium_object(context) select_element = _element(germanium, selector) s = Select(select_element) if text is not None: for single_text in _ensure_list(text): s.deselect_by_visible_text(single_text) if index is not None: for single_index in _ensure_list(index): single_index = int(single_index) s.deselect_by_index(single_index) if value is not None: for single_value in _ensure_list(value): s.deselect_by_value(single_value) if not text and not index and not value: s.deselect_all()
def test_select(self): se = self.driver.find_element_by_id('provise') select = Select(se) sleep(2) # select.select_by_index(2) # sleep(2) # select.select_by_value('tj') # sleep(2) # select.select_by_visible_text('北京') # sleep(2) for i in range(1,6): select.select_by_index(i) sleep(1) print(select.first_selected_option.text) sleep(2) for i in range(6): select.deselect_all() sleep(1) for option in select.options: print(option.text) sleep(1)
async def test_select(start_page, browser): from tests.components.pages.select import page await start_page(page) elem = Select(await browser.wait_for_element_by_css_selector('#select')) multi = Select(await browser.wait_for_element_by_css_selector('#multi')) for i in range(10, 0, -1): elem.select_by_value(str(i)) await browser.wait_for_text_to_equal('#output', str(i)) multi.select_by_value(str(i)) await asyncio.sleep(0.01) output = set( json.loads((await browser.wait_for_element_by_id('multi-output')).text)) assert len(set(range(10, i + 10, -1)).difference(output)) == 0 multi.deselect_all() await asyncio.sleep(0.01) output = set( json.loads((await browser.wait_for_element_by_id('multi-output')).text)) assert len(output) == 0
def test_13_selenium_select_default_organization_option(self): from selenium.webdriver.support.select import Select select_organization = Select(self.filters.new_filter_menu.organizations_field.element) select_organization.deselect_all() select_organization.select_by_visible_text("ACME_Corporation") selected_option_value = select_organization.first_selected_option.get_attribute('value') self.assertElementValue(self.filters.new_filter_menu.organizations_field.element, selected_option_value)
def selector(self, index=None, value=None, text=None, de_index=None, de_value=None, de_text=None, de_all=False): s = Select(self.element) if index: s.select_by_index(index) elif value: s.select_by_value(value) elif text: s.select_by_visible_text(text) elif de_index: s.deselect_by_index(de_index) elif de_value: s.deselect_by_value(de_value) elif de_text: s.deselect_by_visible_text(de_text) elif de_all: s.deselect_all() else: return None
def deselect_all(locatorMethod, locatorExpression, *args): u"""取消勾选所有选中项""" global driver try: select = Select(getElement(driver, locatorMethod, locatorExpression)) select.deselect_all() except Exception, e: raise e
def write_values_on_web_page(driver, field, value): if field == "medio" or field == "pais" or field == "tema": select = Select(driver.find_element_by_name(field)) select.deselect_all() select.select_by_visible_text(value) else: element = driver.find_element_by_name(field) element.clear() element.send_keys(value)
def clear(self, value=""): if self.__tag == "input" or self.__tag == "textarea": self.__element.clear() elif self.__tag == "select": select = Select(self.__element) if value != None: select.deselect_by_value(value) else: select.deselect_all()
def select_option_from_dropdown_menu(driver, field, value, seconds): #Sec1 and sec2 have one option already selected that's why we need to deselect it first if field == "sec1" or field == "sec2": select = Select(driver.find_element_by_id(field)) select.deselect_all() select.select_by_visible_text(value) else: (Select(driver.find_element_by_id(field))).select_by_visible_text(value) time.sleep(seconds)
def deselect(self, input, type='index'): #下拉选 selector = Select(self.element) if type == "index": selector.deselect_by_index(input) elif type == "value": selector.deselect_by_value(input) elif type == "text": selector.deselect_by_visible_text(input) else: selector.deselect_all()
def deselectalloptionsfromdrpdwn(self,locatorvalue,locatortype="id/name/xpath/css/linktext/partiallinktext"): try: element = self.getElement(locatorvalue,locatortype) s1 = Select(element) s1.deselect_all() self.log.info("deselected all options from element with locator type "+locatortype +" and locator value "+locatorvalue) except Exception as e: self.log.error("Unable to deselect all options from element with locator type "+locatortype+" and locator value "+ locatorvalue +str(e)) return False
def deselectalloptions(self, locatorvalue, locator="id"): element = self.getWebelement(locatorvalue, locator) if element is not None: s1 = Select(element) s1.deselect_all() self.log.info("De Selected all options from drp dwn " + locatorvalue) else: self.log.error("Unable to de select option from web element " + locatorvalue)
def test_13_selenium_select_default_organization_option(self): from selenium.webdriver.support.select import Select select_organization = Select( self.filters.new_filter_menu.organizations_field.element) select_organization.deselect_all() select_organization.select_by_visible_text("ACME_Corporation") selected_option_value = select_organization.first_selected_option.get_attribute( 'value') self.assertElementValue( self.filters.new_filter_menu.organizations_field.element, selected_option_value)
def test_multiple_select(self): se = self.driver.find_element_by_id("provise") select = Select(se) for i in range(0, 3): select.select_by_index(i) sleep(2) # 反向选择 select.deselect_all() sleep(2)
def deSelect(self): fruit = op.driver.find_element_by_css_selector( "#multiple-select-example") de = Select(fruit) time.sleep(2) de.deselect_by_visible_text("Orange") time.sleep(2) de.select_by_visible_text("Orange") time.sleep(2) de.deselect_all() time.sleep(2)
def clear_select(self, locator): """ 取消选择的下拉框 :param locator: :return: """ if isinstance(locator, tuple): el = self.get_element(locator) else: el = locator select = Select(el) select.deselect_all()
def test_multi_select(self): self.driver.get("http://cookbook.seleniumacademy.com/Config.html") time.sleep(3) color = Select(self.driver.find_element_by_name("color")) color.deselect_all() color.select_by_index(2) color.select_by_visible_text("Silver") color.select_by_value("br") expected_selected_colors=['White',"Brown","Silver"] actual_selected_colors = [] for one_selected_options in color.all_selected_options: actual_selected_colors.append(one_selected_options) self.assertListEqual(expected_selected_colors,actual_selected_colors)
def Base_select(self, param): """ - name: select 1st select: id: element1 by_index: 1 - name: select by value select: id: element1 by_value: value1 - name: select by visible text select: id: element1 by_text: "text 1" """ elem = self.findone(param) if elem is None: raise Exception("element not found: %s" % (param)) flag = param.get("deselect", False) sel = Select(elem) if "by_index" in param: if flag: sel.deselect_by_index(param.get("by_index")) else: sel.select_by_index(param.get("by_index")) elif "by_value" in param: if flag: sel.deselect_by_value(param.get("by_value")) else: sel.select_by_value(param.get("by_value")) elif "by_text" in param: if flag: sel.deselect_by_visible_text(param.get("by_text")) else: sel.select_by_visible_text(param.get("by_text")) elif param.get("all", False): if flag: sel.deselect_all() else: sel.select_all() retp = param.get("return", "selected") if retp == "selected": res = sel.all_selected_options elif retp == "first": res = sel.first_selected_option elif retp == "all": res = sel.options else: return return self.return_element(param, res)
def test_MultiSelect(self): self.driver.get(self.baseurl2) sel=Select(self.driver.find_element_by_id("fruits")) self.assertEqual(sel.is_multiple, True) sel.select_by_visible_text("Apple") time.sleep(2) sel.deselect_by_value("apple") time.sleep(2) sel.select_by_index(3) sel.select_by_value("orange") print(list(sel.all_selected_options)) time.sleep(2) sel.deselect_all()
def testChildWindow(setUp): mainWindowHandle = driver.current_window_handle windowElement = driver.find_element_by_xpath("//button[contains(text(),'Open Window')]") windowElement.click() Handles = driver.window_handles for handle in Handles: if handle != mainWindowHandle: driver.switch_to.window(handle) time.sleep(5) driver.find_element_by_xpath("//input[contains(@name, 'query')]").send_keys("Python") time.sleep(3) driver.close() driver.switch_to.window(mainWindowHandle) time.sleep(2) multiElement = driver.find_element_by_id("multiple-select-example") multiElementSelection = Select(multiElement) multiElementSelection.deselect_all() time.sleep(2)
def drop_down_multi_select(): url_dropdown = "https://www.seleniumeasy.com/test/basic-select-dropdown-demo.html" driver.get(url_dropdown) ddown_list = driver.find_element_by_id("multi-select") # Select element with '<select>' tag selection = Select(ddown_list) # Multi select drop down enables you to select multi options selection.select_by_value("New York") selection.select_by_visible_text("Ohio") print("selection.all_selected_options :") for element in selection.all_selected_options: print(element.text) # this will return all selected options (new york, ohio) print("Delesecting by index: ") selection.deselect_by_index(4) selection.select_by_index(5) selection.select_by_index(7) print("Deselecting_all : ") selection.deselect_all()
def Multi_Select_drpdwn(self): driver2 = self.driver baseurl = 'http://output.jsbin.com/osebed/2' driver2.get(baseurl) multi_drpdwn = driver2.find_element(By.ID, 'fruits') sel = Select(multi_drpdwn) sel.select_by_visible_text('Banana') time.sleep(2) sel.select_by_value('grape') time.sleep(2) sel.select_by_index(1) time.sleep(2) sel.deselect_all() time.sleep(5) print(sel.all_selected_options) options1 = sel.options for i in range(len(options1)): print(options1[i])
def deselect_by_given(self, locator: str, way: int, value=None): """ 使用给定方式,取消已选下拉框选项(适用于原生select下拉框元素) :param locator: :param way: 1 通过index选取,2 通过value属性值选取,3 通过文本值选取,4 取消全部 :param value: 给定index或value属性值或文本值 :return: """ element_obj = self.find_element(locator) select = Select(element_obj) if way == 1: select.deselect_by_index(value) elif way == 2: select.deselect_by_value(value) elif way == 3: select.deselect_by_visible_text(value) elif way == 4: if select.is_multiple: select.deselect_all() self._log.info('已取消已选下拉框选项')
def clear_form( self, ku_select: Select, ku_critical: WebElement, eku_select: Select, eku_critical: WebElement, tf_select: Select, tf_critical: WebElement, subject: typing.Dict[str, WebElement], cn_in_san: WebElement, ) -> None: """Clear the form.""" ku_select.deselect_all() eku_select.deselect_all() tf_select.deselect_all() if ku_critical.is_selected(): ku_critical.click() if eku_critical.is_selected(): eku_critical.click() if tf_critical.is_selected(): tf_critical.click() if cn_in_san.is_selected(): cn_in_san.click() for field in subject.values(): field.clear()
def selects(self): se = self.driver.find_element_by_name('provide') select = Select(se) # select.select_by_index(0) # # sleep(2) # select.select_by_value("tj") # sleep(2) # select.select_by_visible_text("上海") # sleep(2) for i in range(3): select.select_by_index(i) sleep(2) select.deselect_all() sleep(2) for option in select.options: print(option.text) sleep(2) self.driver.quit()
def browser_launch(self): self.driver.get("file:///C:/Users/shekar/PycharmProjects/Selenium_July/htmlpages/page6.html") self.driver.maximize_window() #Single select drop down ele1 = self.driver.find_element(By.XPATH, "(//select[@name='Countries'])[1]") sel = Select(ele1) sel.select_by_index(1) time.sleep(2) sel.select_by_visible_text("China") time.sleep(2) sel.select_by_value("NZ") #Multi select drop down ele1 = self.driver.find_element(By.XPATH, "(//select[@name='Countries'])[2]") sel = Select(ele1) sel.select_by_index(1) time.sleep(2) sel.select_by_visible_text("China") time.sleep(2) sel.select_by_value("NZ") #deselcting the options in multi select drp dwn time.sleep(2) sel.deselect_by_index(1) time.sleep(2) sel.deselect_by_visible_text("China") time.sleep(2) sel.deselect_by_value("NZ") #Deselecting all options selected in multi select drp dwn sel.deselect_all() time.sleep(10) self.driver.close()
def _select_combo_box(self, combo_box_identifier, option_name=None, page_load=False, timeout=5, option_name_list=None, by=By.NAME): """ DESCRIPTION: Find the select element defined by combo_box_identifier and by, and select the provided option(s). If page_load is True, wait during timeout seconds until a new page is loaded before returning. In case of a multiple selection, clear the previous selection before doing any new selection. If the combo box is a single combo box, and option name is already selected, it will return immediately, even if page_load is set to True. The same happens if the combo box is a multiple combo box, and option_name_list matches exactly the current selected options. INPUT: combo_box_identifier: The identifier used to find the combo box. This parameter and the "by" parameter are used together to create a locator. option_name: The name of a single option to select. Exactly one among option_name and option_name_list must be provided. If option_name is provided and the combo box is a multiple combo box, it is strictly identical as if option_name_list was provided as an iterable of 1 element. The selection is done according to the displayed text of the option, and not according to its value. The default value is None. page_load: If it is True, the function will wait for a new page being loaded before returning. If the combo box is a multiple combo box, and option_name_list contains more than 1 element, it will wait between each selection. The default value is False. timeout: The number of seconds to wait before raising a TimeoutException if a new page does not load. option_name_list: An iterator containing the name of options to select. Exactly one among option_name and option_name_list must be provided. If option_name_list is provided, but the combo box is a single combo box, a RuntimeError is raised, even if option_name_list contains only 1 element. The selection is done according to the displayed text of the option, and not according to its value. The default value is None. by: Defined how the combo box will be looked for. It forms a locator with combo_box_identifier. The default value is By.Name. OUTPUT: Return True if at least one selection has been performed, otherwise False. In other words, return False if the selected option matches with the provided option_name for a single combo box, or if the selected options match with the provided option_name_list for a multiple combo box. ERROR: Raise a RuntimeError if neither or both of option_name and option_name_list are not None. Raise a RuntimeError if option_name_list is provided, but the combo box is a single combo box. Raise a TimeoutException if page_load is True but the page did not refresh in timeout seconds. Raise a StaleElementReferenceException if the combo box is a multiple combo box, more than one selection need to be done and page_load is False whereas the page is refreshed between each selection. Raise a UnexpectedTagNameException when the locator (combo_box_identifier, by) locates an element, but this element is not a combo box. Raise a NoSuchElementException when no element can be referred by the locator (combo_box_idenfifier, by). """ if (option_name is None and option_name_list is None) or (option_name is not None and option_name_list is not None): raise RuntimeError("Exactly 1 among option_name and option_name_list must be None") select_element = self.driver.find_element(by, combo_box_identifier) is_multiple = select_element.get_attribute("multiple") select_element = Select(select_element) if option_name_list is not None and not is_multiple: raise RuntimeError("Can't select multiple value in a single select") if option_name is not None: # If the value is already selected, there is nothing to be done if select_element.first_selected_option.text == option_name: return False option_name_list = [option_name] if is_multiple: if set([option.text for option in select_element.all_selected_options]) == set(option_name_list): return False if page_load: # We have to deselect element one by one, as the select element will stale with deselect_all while len(select_element.all_selected_options) != 0: option = select_element.first_selected_option with self.wait_for_page_load(timeout): select_element.deselect_by_visible_text(option.text) # As the page is reloaded, we have to look again for the element select_element = Select(self.driver.find_element(by, combo_box_identifier)) else: select_element.deselect_all() for option_name in option_name_list: if page_load: with self.wait_for_page_load(timeout): select_element.select_by_visible_text(option_name) # As the page is reloaded, we have to look again for the element in case of multiple selection select_element = Select(self.driver.find_element(by, combo_box_identifier)) else: select_element.select_by_visible_text(option_name) return True
def set_multiselect_field_with_id_containing(field_id, filter_string): select_input = Select(world.browser.find_element_by_id(field_id)) select_input.deselect_all() _select_option_containing(filter_string, select_input)
print("3.Selected option is ", sel.first_selected_option.text) sel.select_by_value("OFFER DECLINED") print("4.Selected option is ", sel.first_selected_option.text) #You can deselect options of a multi-select driver.get( "https://www.jqueryscript.net/demo/Two-side-Multi-Select-Plugin-with-jQuery-multiselect-js/" ) ele = driver.find_element_by_id("undo_redo") sel = Select(ele) print("-----All options--------") for a in sel.options: print(a.text) print("----------------------") print("is it multi select- ", sel.is_multiple) sel.select_by_index(5) print("5.Selected option is ", sel.first_selected_option.text) sel.deselect_by_index(5) print("6.Selected option count is ", len(sel.all_selected_options)) sel.select_by_value("4") sel.select_by_visible_text("PHP") print("7.Selected option count is ", len(sel.all_selected_options)) print("-----All selected options--------") for a in sel.all_selected_options: print(a.text) sel.deselect_all() print("8.Selected option count is ", len(sel.all_selected_options)) driver.quit()
def remove_all_selections(self,locator): locator = self.deal_locator(locator) el = self.driver.find_element(self.getBy(selectLocator), self.getValue(selectLocator)) select = Select(el) select.deselect_all()
#!/usr/bin/env python #@Time :2019-11-01 21:43:17 #@Author :azhenglianxi #@Software :PyCharm #单元框 #怎么判断元素 是不是被选中 from selenium.webdriver.support.select import Select ## male.clock() #来选中元素 # if not bike.is_slelcted(): # bike.click() #复选框 怎么选择 #select 标签的 from Selenium_auto.funtion_Interface import * driver = ChromeDriverBrowser() se = driver.find_element_by_id('') select = Select(se) select.select_by_visible_text('宝马 740') select.deselect_by_index(0) #通过下标选择 从0开始 #全选操作 select.deselect_all() #全部取消选择 select.is_multiple.bit_length() #单选下拉框 select.select_by_visible_text("女")
def deSelectAllValues(element): sel = Select(element) sel.deselect_all()
def deselect_version(self): element = self.selenium.find_element(*self._multiple_version_select_locator) select = Select(element) select.deselect_all()
def dropdown_deselectall(self, element): varA = Select(element) varA.deselect_all()