python-selenium对下拉选择框的操作,Select类
2022/7/27 14:23:01
本文主要是介绍python-selenium对下拉选择框的操作,Select类,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
首先要确认下拉选择框,是通过html中的select-option标签组合而成的
**Select类只能操作select-option组成的下拉选择框**,例如<select id="s1Id"> <option></option> <option value="o1" id="id1">o1</option> <option value="o2" id="id2">o2</option> <option value="o3" id="id3">o3</option> </select>
python中使用Select类操作下拉选择框,要选导入该类
from selenium.webdriver.support.select import Select
# 现获取select标签的webelement对象 ele = self.driver.find_element(By.ID, 's1Id') # 再创建操作下拉框的对象 select = Select(ele) # 通过索引选择下拉选项 # select.select_by_index(1) # 通过文本内容选择下拉选项 # select.select_by_value('o3') # 所有选项的webelement对象,返回一个列表 # print(select.options) # 所有选中的选项的webelement对象,返回一个列表 print(select.all_selected_options)
常用方法/属性
方法/属性 | 描述 |
---|---|
select_by_value() | 根据值选择 |
select_by_index() | 根据索引选择 |
select_by_visible_text | 根据文本选择 |
deselect_by_value | 根据值反选 |
deselect_by_index | 根据索引反选 |
deselect_by_visible_text | 根据文本反选 |
deselect_all | 反选所有 |
options | 所有选项的webelement对象 |
all_selected_options | 所有选中的选项的webelement对象 |
first_selected_option | 第一个选择选项 |
这篇关于python-selenium对下拉选择框的操作,Select类的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-05-08有遇到过吗?同样的规则 Excel 中 比Python 结果大
- 2024-03-30开始python成长之路
- 2024-03-29python optparse
- 2024-03-29python map 函数
- 2024-03-20invalid format specifier python
- 2024-03-18pool.map python
- 2024-03-18threads in python
- 2024-03-14python Ai 应用开发基础训练,字符串,字典,文件
- 2024-03-13id3 algorithm python
- 2024-03-13sum array elements python