1 # -*- coding:utf-8 -*- 2 3 """ 4 下拉列表处理 5 """ 6 7 from selenium import webdriver 8 from selenium.webdriver.support.select import Select 9 10 driver = webdriver.Firefox() 11 12 driver.get(r'F:电子书seleniumSelenium Testing Tools Cookbook(中文)demoDroplist.html') 13 14 #得到下拉列表框 15 make = Select(driver.find_element_by_name('make')) 16 17 #获取下拉列表框的数量 18 print len(make.options) 19 20 #通过 value 属性来选择项 21 make.select_by_visible_text('Audi') 22 23 #通过索引来选择项,索引值从0开始 24 make.select_by_index(2) 25 26 make.select_by_value('honda')
1 # -*- coding:utf-8 -*- 2 3 from selenium import webdriver 4 from selenium.webdriver.support.select import Select 5 6 driver = webdriver.Firefox() 7 8 driver.get('F:电子书seleniumSelenium Testing Tools Cookbook(中文)demoDroplist.html') 9 10 color = Select(driver.find_element_by_name('color')) 11 12 #验证下拉列表支持多选 13 print color.is_multiple 14 15 #获取下拉列表的数量 16 print len(color.options) 17 18 #使用可见的文本年来选择项 19 color.select_by_visible_text('Black') 20 color.select_by_visible_text('Red') 21 color.select_by_visible_text('Silver') 22 23 #通过可见的文本取消已选选项 24 color.deselect_by_visible_text('Silver') 25 26 #通过选项索引取消已选选项 27 color.deselect_by_index(0) 28 29 for x in color.options: 30 print x.text