selenium (三) 下拉框选项操作

对下拉框操作的方式其实有多种,可以先通过find_elements_by_xpath()获取到下拉框中的所有选项列表,然后在通过
list元素进行click()来选择选项(这是我最初实现对下拉框操作的方式),也可以使用selenium自带的方法实现
下拉框选项的操作。两者其实差不多,至于要用哪种方式,这个看个人喜好。
以下selenium自带的下拉框处理方法。


selenium中有一个类单独提供对下拉框的操作:
from selenium.webdriver.support.ui import Select

获取下拉框的所有选项,返回一个元素列表
Select(selectLabel).options
实例:
element_province = driver.find_element_by_xpath('//td[@id="area"]/select[@id="province"]') #获取select标签
province_Alloptions = Select(element_province).options
area = []
for i in province_Alloptions:
#将下拉框文本值,添加到列表中
area.append(i.text)

for value in area:
#打印下拉框列表的值
print value


获取下拉框的第一个选项,也就是下拉框的默认值
Select(selectLabel).first_selected_option
实例:
element_city = driver.find_element_by_xpath('//td[@id="area"]/select[@id="city"]') #获取select标签
city_options = Select(element_city).first_selected_option
print city_options.text

通过下拉框的value属性值来选择选项:
select_by_value()
实例:
element_province = driver.find_element_by_xpath('//td[@id="area"]/select[@id="province"]')
Select(element_province).select_by_value("140000") #选择value值为:140000选项,通过value值选择选项

通过下拉框的选项索引来选择选项:
select_by_index()
实例:
element_city = driver.find_element_by_xpath('//td[@id="area"]/select[@id="city"]')
Select(element_city).select_by_index(1) #选择第2个选项,通过下拉框索引选择选项

通过下拉框的选项名来选择选项:
select_by_visible_text()
element_county = driver.find_element_by_xpath('//td[@id="area"]/select[@id="county"]')
county_Alloption = Select(element_county).options
Select(element_county).select_by_visible_text(u"古交市")


实例代码:

#-*-encoding:utf-8-*-
from selenium import webdriver
from time import sleep
from selenium.webdriver.support.ui import Select

driver=webdriver.Chrome()
driver.get("http://testshop:8081/tshop/index.php?con=simple&act=login")

#登录
def login_shop(userName,pwd):
	try:
		element_account=driver.find_element_by_id("account")   #通过id定位
		element_account.send_keys(userName)
		element_password=driver.find_element_by_name("password")  #通过name定位
		element_password.send_keys(pwd)
		element_loginBtn=driver.find_element_by_xpath('//button[@class="btn btn-main "]')  #通过xpath定位
		element_loginBtn.click()
	except Exception as e:
		driver.save_screenshot('F:\testAndStudy\test\login_shop.png')


def click_myOrder():
	#点击【我的订单】链接
	sleep(1)
	try:
		element_myOrder=driver.find_element_by_link_text(u"我的订单")
		element_myOrder.click()
	except Exception as e:
		driver.save_screenshot("F:\testAndStudy\test\click_myOrder.png")


def click_personalInfor():
	#点击【个人资料】菜单
	sleep(1)
	try:
		element_personalInfo=driver.find_element_by_link_text(u'个人资料')
		element_personalInfo.click()
	except Exception as e:
		driver.save_screenshot("F:\testAndStudy\test\click_personalInfor.png")


def click_areaOption():
	#点击基本资料页面选择所在地址下拉框
	sleep(1)
	try:
		#选择省份
		element_province = driver.find_element_by_xpath('//td[@id="area"]/select[@id="province"]')
		Select(element_province).select_by_value("140000")    #选择value值为:140000选项,通过value值选择选项

		province_Alloptions = Select(element_province).options
		area=[]
		for i in province_Alloptions:
			#将下拉框文本值,添加到列表中
			area.append(i.text)

		for value in area:
			#打印下拉框列表的值
			print value


		#选择城市
		element_city = driver.find_element_by_xpath('//td[@id="area"]/select[@id="city"]')
		Select(element_city).select_by_index(1)      #选择第2个选项,通过下拉框索引选择选项

		city_options = Select(element_city).first_selected_option
		print city_options.text   #打印第1个选项


		#选择县/区
		element_county = driver.find_element_by_xpath('//td[@id="area"]/select[@id="county"]')
		county_Alloption = Select(element_county).options
		Select(element_county).select_by_visible_text(county_Alloption[len(county_Alloption)-1].text)   #选择最后一个option的值,通过option值选择选项
		county=county_Alloption[len(county_Alloption)-1].text   #打印选择的选项
		print county

	except Exception as e:
		print e
		driver.save_screenshot("F:\testAndStudy\test\click_areaOption.png")




login_shop(userName="123@qq.com",pwd="123456")  
click_myOrder()
click_personalInfor()
click_areaOption()

  

业务场景 -->> 完善个人资料:

原文地址:https://www.cnblogs.com/JcHome/p/10802817.html