vue如何取消下拉框按回车自动下拉-尊龙游戏旗舰厅官网
在web自动化测试中,经常会遇到下拉框,对列出的选项进行选择,或者判断选择的选项,本文将介绍如何使用selenium去操作下拉框,实现自动化测试。
测试url:http://sahitest.com/demo/selecttest.htm
使用webelement类的send_keys(value)方法也可以选择下拉框(select标签)的值,但它只能使用选项的value值来定位:
self.driver.get("http://sahitest.com/demo/selecttest.htm")ele = self.driver.find_element_by_id('s3id')print(ele.text) # 打印select所有选项值ele.send_keys("o2val") # 选择value=o2val 的选项print(ele.get_attribute("value")) # 打印所选择的value值结果:
o1o2o3 with spaces with nbspo2valselect类所有方法:
select_by_index(index):通过选项index选择
select_by_value(value):通过选项value值选择
select_by_visible_text(text):过显示的文本选择
deselect_by_index(index):取消选择,用于多选
deselect_by_value(value):取消选择,用于多选
deselect_by_visible_text(text):取消选择,用于多选
deselect_all():全部取消,用于多选
options:所有选项
first_selected_option:第一个选择的选项(多选情况下)或者当前选择的选项(单选)
all_selected_options:所有已经选择的选项
select_by_index、select_by_value、select_by_visible_text三种方法选择
first_selected_option.text返回当前所选择的选项值
self.driver.get("http://sahitest.com/demo/selecttest.htm")ele = self.driver.find_element_by_id('s3id')selected_element = select(ele) # 实例化selectselected_element.select_by_index(1)# for select in selected_element.all_selected_options:# print(select.text)print(selected_element.first_selected_option.text) # 打印当前选择的选项值sleep(1)selected_element.select_by_value("o2val")print(selected_element.first_selected_option.text)sleep(1)selected_element.select_by_visible_text("o3")print(selected_element.first_selected_option.text)sleep(1)结果:
o1 o2 o3取消选择deselect不能用于下拉框选择,只能用于多选,即属性multiple="multiple"的select标签。
取消选择有deselect_by_index、deselect_by_value、deselect_by_visible_text、deselect_all四种方法
all_selected_options返回所有已选择的选项
self.driver.get("http://sahitest.com/demo/selecttest.htm")## 多选ele2 = self.driver.find_element_by_id('s4id')selected_element2 = select(ele2) # 实例化selectselected_element2.select_by_index(1)selected_element2.select_by_index(2)selected_element2.select_by_index(3)print("######")for select in selected_element2.all_selected_options: print(select.text)print("######")selected_element2.deselect_by_index(1)for select in selected_element2.all_selected_options: print(select.text)print("######")selected_element2.deselect_by_value("o2val")for select in selected_element2.all_selected_options: print(select.text)print("######")selected_element2.deselect_by_visible_text("o3")for select in selected_element2.all_selected_options: print(select.text)结果:
######o1o2o3######o2o3######o3######总结
以上是尊龙游戏旗舰厅官网为你收集整理的vue如何取消下拉框按回车自动下拉_web自动化测试 | select下拉框的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇:
- 下一篇: