标签:ted ble int 功能 com val 使用 sel python
web网页大多选择功能使用下拉列表方式实现,在selenium进行自动化过程中,可通过Select类实现下拉列表的多种操作。
select_by_index() # 通过索引定位-选项 select_by_value() # 通过value值定位-选项 select_by_visible_text() # 通过文本值定位-选项 deselect_all() # 取消所有选项 deselect_by_index() # 取消对应index选项 deselect_by_value() # 取消对应value选项 deselect_by_visible_text() # 取消对应文本选项 options() # 返回下拉列表-所有选项 first_selected_option() # 返回下拉列表-第一个选项 all_selected_options() # 返回下拉列表-选中的选项
备注:
使用时需先导入Select类:from selenium.webdriver.support.select import Select
携程首页-酒店-房间数选择,采用下拉列表形式。模拟携程酒店-房间,勾选房间数如下:
1 # 模拟携程首页-酒店,勾选房间数 2 from selenium.webdriver.support.select import Select 3 from selenium import webdriver 4 from time import sleep 5 6 driver.get("https://www.ctrip.com/?sid=155952&allianceid=4897&ouid=index") # 进入携程首页 7 sleep(3) 8 9 select_ele = driver.find_element_by_id("J_roomCountList") # 定位房间数下拉框 10 select_ele.click() # 点击展开房间数下拉列表 11 sleep(2) 12 Select(select_ele).select_by_index(2) # 选中选项-3间(索引从0开始) 13 # Select(select_ele).select_by_value(3) # 选中选项-3间,value=3 14 # Select(select_ele).select_by_visible_text("3间") # 选中-3间,文本"3间" 15 sleep(3)
获取下拉列表选项:
1 select_ele = driver.find_element_by_id("J_roomCountList") # 定位房间数下拉框 2 select_ele.click() # 点击展开房间数下拉列表 3 4 # 获取所有下拉列表选项 5 for select in Select(select_ele).options: 6 print(select.text) 7 8 # 获取选中的选项 9 for select in Select(select_ele).all_selected_options: 10 print("选中选项:", select.text)
(selenium+python)_UI自动化05_定位select下拉列表
标签:ted ble int 功能 com val 使用 sel python
原文地址:https://www.cnblogs.com/mini-monkey/p/12109680.html