def test_operateMultipleOptionDropList(self): url = "http://127.0.0.1/test_multiple_select.html" # 访问自定义的html网页 self.driver.get(url) # 导入Select模块 from selenium.webdriver.support.ui import Select # 使用xpath定位方式获取select页面元素对象 select_element = Select(self.driver.find_element_by_xpath("//select")) # 通过序号选择第一个元素 select_element.select_by_index(0) # 通过选项的文本选择“山楂”选项 select_element.select_by_visible_text("山楂") # 通过选项的value属性值选择value=“mihoutao”的选项 select_element.select_by_value("mihoutao") # 打印所有的选中项文本 for option in select_element.all_selected_options: print option.text # 取消所有已选中项 select_element.deselect_all() time.sleep(2) print u"-----------再次选中3个选项--------------" select_element.select_by_index(1) select_element.select_by_visible_text("荔枝") select_element.select_by_value("juzi") # 通过选项文本取消已选中的文本为“荔枝”选项 select_element.deselect_by_visible_text("荔枝") # 通过序号取消已选中的序号为1的选项 select_element.deselect_by_index(1) # 通过选项的value属性值取消已选中的value=“juzi”的选项 select_element.deselect_by_value("juzi")
def test_operMutipleoptionsDropList(self): URL = 'F:\gitstorehouse\selenium3.0\webdriverAPI接口\测试页面\下拉框.html' self.driver.get(URL) from selenium.webdriver.support.ui import Select select_element = Select(self.driver.find_element_by_name('fruit')) #选择元素,多选 select_element.select_by_index(1) select_element.select_by_visible_text('山楂') select_element.select_by_value('lizhi') for optins in select_element.all_selected_options: print(optins.text) #取消所选元素 select_element.deselect_all() for optins in select_element.all_selected_options: print(optins.text) sleep(5) print('###########################################') #重新选择元素 select_element.select_by_index(2) select_element.select_by_visible_text('山楂') select_element.select_by_value('lizhi') for optins in select_element.all_selected_options: print(optins.text) sleep(2) #取消选择元素 select_element.deselect_by_index(1) select_element.deselect_by_value('lizhi') select_element.deselect_by_visible_text('山楂') sleep(5)
def element_deselect(self, cls, type, value): '''取消下拉框勾选操作''' select = Select(self.get_element(cls)) if (type == 'index'): select.deselect_by_index(value) elif (type == 'visible_text'): select.deselect_by_visible_text(value) elif (type == 'value'): select.dedeselect_by_value(value)
def callback(elements): s = Select(elements.item) if i is not None: s.deselect_by_index(i) elif value is not None: s.deselect_by_value(value) elif text is not None: s.deselect_by_visible_text(text) else: s.deselect_all()
def Select_Options(): select = Select(driver.find_elements_by_id()) select.select_by_index() select.select_by_value() select.select_by_visible_text() select.deselect_by_index() select.deselect_by_value() select.deselect_by_visible_text() select.deselect_all() select.all_selected_options select.first_selected_option select.options
def testDeselectByVisibleTextMultiple(driver, pages): pages.load("formPage.html") for select in [multiSelectValues1, multiSelectValues2]: sel = Select(driver.find_element(By.NAME, select['name'])) sel.deselect_all() sel.select_by_index(0) sel.select_by_index(1) sel.select_by_index(2) sel.select_by_index(3) sel.deselect_by_visible_text(select['values'][1]) sel.deselect_by_visible_text(select['values'][3]) selected = sel.all_selected_options assert len(selected) == 2 assert selected[0].text == select['values'][0] assert selected[1].text == select['values'][2]
def testDeselectByVisibleTextMultiple(self): if self.driver.capabilities['browserName'] == 'chrome' and int(self.driver.capabilities['version'].split('.')[0]) < 16: pytest.skip("deselecting preselected values only works on chrome >= 16") self._loadPage("formPage") for select in [multiSelectValues1, multiSelectValues2]: sel = Select(self.driver.find_element(By.NAME, select['name'])) sel.deselect_all() sel.select_by_index(0) sel.select_by_index(1) sel.select_by_index(2) sel.select_by_index(3) sel.deselect_by_visible_text(select['values'][1]) sel.deselect_by_visible_text(select['values'][3]) selected = sel.all_selected_options self.assertEqual(len(selected), 2) self.assertEqual(selected[0].text, select['values'][0]) self.assertEqual(selected[1].text, select['values'][2])
def step_impl(context, dr_program_name, site_name): br = context.browser br.find_element_by_link_text("Overview").click() assert br.current_url.endswith("vtn/home/") != -1 br.find_element_by_link_text(dr_program_name).click() select = Select(br.find_element_by_name('sites')) select.deselect_by_visible_text(site_name) print(site_name) # all_selected_options = select.all_selected_options # selected_texts = [option.text for option in all_selected_options] # select.deselect_all() # for text in selected_texts: # if text != site_name: # select.select_by_visible_text(text) br.find_element_by_name("Save").click()
def testDeselectByVisibleTextMultiple(self, driver, pages): if driver.capabilities['browserName'] == 'chrome' and int(driver.capabilities['version'].split('.')[0]) < 16: pytest.skip("deselecting preselected values only works on chrome >= 16") pages.load("formPage.html") for select in [multiSelectValues1, multiSelectValues2]: sel = Select(driver.find_element(By.NAME, select['name'])) sel.deselect_all() sel.select_by_index(0) sel.select_by_index(1) sel.select_by_index(2) sel.select_by_index(3) sel.deselect_by_visible_text(select['values'][1]) sel.deselect_by_visible_text(select['values'][3]) selected = sel.all_selected_options assert len(selected) == 2 assert selected[0].text == select['values'][0] assert selected[1].text == select['values'][2]
def test_operateMultipleOptionDropList(self): url = 'F:\\HTMLTest\\select.html' self.driver.get(url) select_element = Select(self.driver.find_element_by_xpath('//select')) select_element.select_by_index(0) select_element.select_by_visible_text('山楂') select_element.select_by_value('mihoutao') for option in select_element.all_selected_options: print(option.text) select_element.deselect_all() time.sleep(2) print('---------------- 再次选中 3 个选项 ------------------') select_element.select_by_index(1) select_element.select_by_visible_text('荔枝') select_element.select_by_value('juzi') select_element.deselect_by_visible_text('荔枝') select_element.deselect_by_index(1) select_element.deselect_by_value('juzi')
def testOperateMultiDropdownlist(self): driver = self.driver WebSite = os.path.join(os.path.abspath('..'), 'Html', '10.23.html') driver.get(WebSite) time.sleep(2) #等待2秒 from selenium.webdriver.support.ui import Select #导入Select模块 Dropdownlist = Select( driver.find_element_by_xpath("//select")) #用xpath方法定位多选下拉列表 time.sleep(2) Dropdownlist.select_by_index(0) #使用select_by_index()方法,通过序号选择第一个元素 time.sleep(2) Dropdownlist.select_by_visible_text( 'Jenson Button' ) #使用select_by_visible_text()方法,通过选项的文本选择"Jenson Button" time.sleep(2) Dropdownlist.select_by_value( 'Ham') #使用select_by_value()方法,通过选项的value值'Ham',选择"Lewis Hamilton" time.sleep(2) for MyDriver in Dropdownlist.all_selected_options: #遍历循环,利用all_selected_options方法,打印所有已经被选中的选项 print(MyDriver.text) time.sleep(2) Dropdownlist.deselect_all() #使用deselect_all()方法,取消选择所有已选中的内容 time.sleep(2) Dropdownlist.select_by_index(1) #随意选择三个选项 Dropdownlist.select_by_visible_text('Sebastian Vettel') Dropdownlist.select_by_value('Rai') time.sleep(2) Dropdownlist.deselect_by_index( 1) #使用deselect_by_index()方法,通过序号取消选择第二个元素 Dropdownlist.deselect_by_visible_text( 'Sebastian Vettel' ) #使用deselect_by_visible_text()方法,通过选项的文本取消选择"Sebastian Vettel" Dropdownlist.deselect_by_value( 'Rai' ) #使用deselect_by_value()方法,通过选项的value值'Rai',取消选择"Kimi Raikkonen" time.sleep(2)
def test_select(self): driver = self.driver # selection 태그에서 옵션의 개수 확인 cars = Select(driver.find_element_by_name("multi_cars")) self.assertEqual(4, len(cars.options)) # ctrl + click 효과 _ multiple choice cars.select_by_visible_text("Volvo") cars.select_by_value("multi_saab") cars.select_by_index(2) cars.select_by_index(3) time.sleep(2) # 선택 취소 cars.deselect_by_visible_text("Volvo") cars.deselect_by_value("multi_saab") cars.deselect_by_index(2) cars.deselect_by_index(3) time.sleep(2)
def testDeselectByVisibleTextMultiple(self, driver, pages): if driver.capabilities['browserName'] == 'chrome' and int( driver.capabilities['version'].split('.')[0]) < 16: pytest.skip( "deselecting preselected values only works on chrome >= 16") pages.load("formPage.html") for select in [multiSelectValues1, multiSelectValues2]: sel = Select(driver.find_element(By.NAME, select['name'])) sel.deselect_all() sel.select_by_index(0) sel.select_by_index(1) sel.select_by_index(2) sel.select_by_index(3) sel.deselect_by_visible_text(select['values'][1]) sel.deselect_by_visible_text(select['values'][3]) selected = sel.all_selected_options assert len(selected) == 2 assert selected[0].text == select['values'][0] assert selected[1].text == select['values'][2]
def test_operateMultipleOptionDropList(self): url = "file:///C:/Users/Administrator/Desktop/seleniumTestCase/23test.html" self.driver.get(url) from selenium.webdriver.support.ui import Select select_element = Select(self.driver.find_element_by_xpath("//select")) select_element.select_by_visible_text("shanzha") select_element.select_by_value("mihoutao") for option in select_element.select.all_selected_options: print option.text select_element.deselect_all() time.sleep(2) print "----3----" select_element.select_by_index() select_element.select_by_visible_text("lizhi") select_element.select_by_value("juzi") select_element.deselect_by_visible_text("lizhi") select_element.deselect_by_index(1) select_element.deselect_by_value("juzi") time.sleep(3)
def test_multiple_selection_in_list(self): driver = self.driver #Get the Dropdown as a Select using it's name attribute color = Select(driver.find_element_by_name("color")) #Verify Dropdown has four options for selection self.assertEqual(5, len(color.options)) #Select multiple options in the list using visible text color.select_by_visible_text("Black") color.select_by_visible_text("Red") color.select_by_visible_text("Silver") #Deselect an option using visible text color.deselect_by_visible_text("Silver") #Deselect an option using value attribute of the option color.deselect_by_value("rd") #Deselect an option using index of the option color.deselect_by_index(0)
def test_multiple_selection_in_list(self): driver = self.driver #Get the Dropdown as a Select using it's name attribute color = Select(driver.find_element_by_name("color")) #Verify Dropdown has four options for selection self.assertEqual(5,len(color.options)) #Select multiple options in the list using visible text color.select_by_visible_text("Black") color.select_by_visible_text("Red") color.select_by_visible_text("Silver") #Deselect an option using visible text color.deselect_by_visible_text("Silver") #Deselect an option using value attribute of the option color.deselect_by_value("rd") #Deselect an option using index of the option color.deselect_by_index(0)
# coding=utf-8 from selenium import webdriver driver = webdriver.Chrome(r"d:\tools\webdrivers\chromedriver.exe") driver.get( 'file:///C:/Users/Administrator/Dropbox/python_autotest/autoUI_selenium/lesson04/ms.html' ) # 打开网址 # 导入 Select from selenium.webdriver.support.ui import Select # 获得相应的WebElement select = Select(driver.find_element_by_id("multi")) # 先去选择所有的 选项 select.deselect_all() select.select_by_visible_text("雅阁") select.select_by_visible_text("奥迪A6") select.deselect_by_visible_text("雅阁") # 获得相应的WebElement select = Select(driver.find_element_by_id("single")) # select.select_by_visible_text("男") select.select_by_visible_text("女") # select.deselect_all() input('press any key to quit...') driver.quit() # 浏览器退出
def test_can_create_edit_delete_player(self): # Edith has logged in and clicks new player link new_button = self.browser.find_element_by_name('create_figure') new_button.click() sleep(3) # Edith enters name, st, and dx. inputbox = self.browser.find_element_by_id('id_figure_name') inputbox.send_keys('Wulf') inputbox.send_keys(Keys.TAB) inputbox = self.browser.find_element_by_id('id_strength') inputbox.send_keys('12') inputbox.send_keys(Keys.TAB) inputbox = self.browser.find_element_by_id('id_dexterity') inputbox.send_keys('12') inputbox.send_keys(Keys.TAB) labels = [ 'Short Sword', ] select = Select(self.browser.find_element_by_id('id_items')) for label in labels: select.select_by_visible_text(label) self.browser.find_element_by_xpath( "//input[@type='submit' and @value='Submit']").click() sleep(3) # Edith is taken back to lobby with new user showing. header_text = self.browser.find_element_by_tag_name('h1').text self.assertIn('WayLame: The Blantasy Blip!', header_text) sleep(3) # Edith decides to change Wulf so clicks edit fignames = [ 'Wulf', ] player_list = self.browser.find_element_by_id('playerList') player_links = player_list.find_elements_by_css_selector('.action') for link in player_links: if 'Wulf' in link.text: link.click() break sleep(3) # Edith selects lon sword but is rejected due to st select = Select(self.browser.find_element_by_id('id_items')) select.select_by_visible_text('Two-Handed Sword') select.deselect_by_visible_text('Short Sword') sleep(3) self.browser.find_element_by_xpath( "//input[@type='submit' and @value='Submit']").click() sleep(3) # Edith changes the st and dx and re-selects the long sword inputbox = self.browser.find_element_by_id('id_strength') inputbox.clear() inputbox.send_keys('14') inputbox.send_keys(Keys.TAB) inputbox = self.browser.find_element_by_id('id_dexterity') inputbox.send_keys('10') #inputbox.send_keys(Keys.TAB) select = Select(self.browser.find_element_by_id('id_items')) select.select_by_visible_text('Two-Handed Sword') self.browser.find_element_by_xpath( "//input[@type='submit' and @value='Submit']").click() sleep(3) # Edith starts a new game self.browser.find_element_by_id("create_game").click() sleep(3) # Edith decides she wants to delete the game game_list = self.browser.find_element_by_id('gameList') game_links = game_list.find_elements_by_css_selector('.action') for link in game_links: if 'Delete' in link.text: link.click() break sleep(3)
def select_option(self, obj): # Set local parameters step = obj.get('order') identifier_dict = obj.get('identifier') if identifier_dict is None: raise Exception('No identifier') child_window = obj.get('child_window') if child_window is None: raise Exception('No child_window') selection_value = obj.get('selection_value') if selection_value is None: raise Exception('No selection_value') selection_type = obj.get('selection_type') if selection_type is None: raise Exception('No selection_type') multiline = obj.get('multiline') action = None element = self.ei.uniquely_identify_element(identifier_dict) # Report no element found if element is None: action = 'No element found' return action if element is not None: # Prepare element for action self.__wait_for_post_back_to_complete(element, obj.get('timeout')) self.driver.execute_script('return arguments[0].scrollIntoView();', element) self.driver.execute_script(' window.scrollBy(0, -100);') selection = Select(element) options = selection.options acted_upon_options = self.__determine_acted_upon_option(options, selection_value, selection_type) # Determine how we are interacting with the select if selection_type.lower() == "index": action = "Select '" + acted_upon_options + "' from the '" + str( self.find_html_for(element.get_attribute('id'))) + "' dropdown" selection.select_by_index(selection_value) elif selection_type.lower() == "text": action = "Select '" + acted_upon_options + "' from the '" + str( self.find_html_for(element.get_attribute('id'))) + "' dropdown" selection.select_by_visible_text(selection_value) elif selection_type.lower() == "value": action = "Select '" + acted_upon_options + "' from the '" + str( self.find_html_for(element.get_attribute('id'))) + "' dropdown" selection.select_by_value(selection_value) elif selection_type.lower() == "deindex": action = "Deselect '" + acted_upon_options + "' from the '" + str( self.find_html_for(element.get_attribute('id'))) + "' dropdown" selection.deselect_by_index(selection_value) elif selection_type.lower() == "detext": action = "Deselect '" + acted_upon_options + "' from the '" + str( self.find_html_for(element.get_attribute('id'))) + "' dropdown" selection.deselect_by_visible_text(selection_value) elif selection_type.lower() == "devalue": action = "Deselect '" + acted_upon_options + "' from the '" + str( self.find_html_for(element.get_attribute('id'))) + "' dropdown" selection.deselect_by_value(selection_value) elif selection_type.lower == "deall": action = "Deselected all values from the '" + str( self.find_html_for(element.get_attribute('id'))) + "' dropdown" selection.deselect_all() else: # This should really just be reported and not raise an exception raise Exception("Unknown selection type.") # Determine if action changes browser count if child_window: self.change_window_connection() print(action) return action
driver.find_element_by_xpath('//a[contains(@href,"dropdown")]').click() dropdown = driver.find_element_by_xpath('//select[@name="country"]') select = Select(dropdown) select.select_by_index(3) time.sleep(4) select.select_by_value("IN") time.sleep(3) select.select_by_visible_text("Australia") time.sleep(2) dropdown_multi = driver.find_element_by_xpath('//select[@id="multiSelect"]') select = Select(dropdown_multi) select.select_by_value("AS") time.sleep(2) select.select_by_index(2) time.sleep(3) select.select_by_visible_text("Angola") select.select_by_value("IN") time.sleep(5) select.deselect_by_visible_text("Albania") select.deselect_by_index(6) select.deselect_by_value("AS") # print(select.first_selected_option.text) # all_selected_options = select.all_selected_options # print(all_selected_options) # select.deselect_all() # # for current_option in all_selected_options: # print(current_option.text)
class Select(Action): def __init__(self, driver, locator=None, element=None, wait_time=10, visible=False): super().__init__(driver, locator, element, wait_time, visible) self.dropdown = Select_(self.element) def select_all(self): all_elements = self.dropdown.options if not all_elements: return for ele in all_elements: if not ele.is_selected(): ele.click() def select_by_visible_texts(self, values=""): values = self._convert(values) for value in values: self.dropdown.select_by_visible_text(value) return self def select_by_indices(self, indices): indices = self._convert(indices) for index in indices: self.dropdown.select_by_index(index) return self def select_by_values(self, values=""): values = self._convert(values) for value in values: self.dropdown.select_by_value(value) return self def deselect_all(self): self.dropdown.deselect_all() def deselect_by_visible_texts(self, values=""): values = self._convert(values) for value in values: self.dropdown.deselect_by_visible_text(value) return self def deselect_by_indices(self, indices): indices = self._convert(indices) for index in indices: self.dropdown.deselect_by_index(index) return self def deselect_by_values(self, values=""): values = self._convert(values) for value in values: self.dropdown.deselect_by_value(value) return self @property def options_text(self): #get can be text, value, element all_elements = self.dropdown.options results = self._get_options(all_elements, "text") return results @property def options_value(self): #get can be text, value, element all_elements = self.dropdown.options results = self._get_options(all_elements, "value") return results @property def options_element(self): #get can be text, value, element all_elements = self.dropdown.options results = self._get_options(all_elements, "element") return results @property def all_selected_options_text(self): #get can be text, value, element all_elements = self.dropdown.all_selected_options results = self._get_options(all_elements, "text") return results @property def all_selected_options_value(self): #get can be text, value, element all_elements = self.dropdown.all_selected_options results = self._get_options(all_elements, "value") return results @property def all_selected_options_element(self): #get can be text, value, element all_elements = self.dropdown.all_selected_options results = self._get_options(all_elements, "element") return results def _get_options(self, all_elements, get): results = [] if not all_elements: return results if "text" in get.strip().lower(): for ele in all_elements: results.append(ele.text) elif "value" in get.strip().lower(): for ele in all_elements: results.append(ele.get_attribute('value')) elif "element" in get.strip().lower(): results = all_elements else: get_logger().error("Invalid input. Valid values for get - text, value, element") raise InvalidArgumentError("Invalid input. Valid values for get - text, value, element") return results def _convert(self, values): if not values: values = [] if not isinstance(values, list): values = [values] return values
def test_deselect_option_multiple(self): select_box = Select(self.driver.find_element_by_name("test_framework")) select_box.select_by_visible_text("Selenium") select_box.select_by_value("rwebspec") select_box.deselect_by_visible_text("RWebSpec") # by label select_box.deselect_by_index(3) # :index
class Select(BaseElement): """ A web select """ def __init__(self, element): BaseElement.__init__(self, element) self._select = WDSelect(self._element) @property def options(self): """ obtains all options in the select element :return: all options in the select element :rtype: list """ inner_options = self._select.options options = [] for option_se in inner_options: options.append(selenium_web_element_to_element(option_se)) return options @property def selected_options(self): """ obtains all options selected :return: all options selected in the select element :rtype: list """ inner_options = self._select.all_selected_options options = [] for option_se in inner_options: options.append(selenium_web_element_to_element(option_se)) return options @property def first_selected_option(self): """ obtains the first option selected :return: the first option selected in the select element :rtype: BaseElement """ option = self._select.first_selected_option return selenium_web_element_to_element(option) def select_by_index(self, index): """ Selects an option by its index :param index: the index to be searched """ return self._select.select_by_index(index) def select_by_value(self, value): """ Selects an option by its value :param value: the value to be searched """ return self._select.select_by_value(value) def select_by_visible_text(self, text): """ Selects an option by its visible text :param text: the value to be searched """ return self._select.select_by_visible_text(text) def deselect_all(self): """ Deselects all selected options """ self._select.deselect_all() def deselect_by_index(self, index): """ Deselects an option by its index :param index: the index to be searched """ self._select.deselect_by_index(index) def deselect_by_value(self, value): """ Selects an option by its value :param value: the value to be searched """ self._select.deselect_by_value(value) def deselect_by_visible_text(self, text): """ Deselects an option by its visible text :param text: the value to be searched """ self._select.deselect_by_visible_text(text)