1. 选择某项的3种方法
- select_by_value(value) :通过option标签中的value属性值选择
- select_by_index(index):通过索引选择,索引从0开始
- select_by_visible_text(text):通过option标签中的文本选择
说明:Select(driver.find_element_by_tag_name("select")).select_by_index(2) :Select()类,需要传入要操作的select对象
1. # -*-coding:utf-8一*- 2. # @Time:2021/1/14 3. # @Author: 大海 4. 5. from selenium import webdriver 6. from selenium.webdriver.support.select import Select 7. 8. driver = webdriver.Chrome() 9. 10. # 未找到练习的网址,中间过程省略...... 11. 12. # 先定位到Select 13. select_ele = driver.find_element_by_tag_name("select") 14. 15. # 根据索引选择 option的索引值,从索引从0开始 16. Select(select_ele).select_by_index(1) 17. 18. # 根据value值选择 <option value="foo">Bar</option> 19. Select(select_ele).select_by_value("foo") 20. 21. # 根据文本值选择 <option value="foo">Bar</option> 22. Select(select_ele).select_by_visible_text("Bar")
2. 返回options信息的方法
- options():返回select所有option选项的元素列表
- all_selected_options():返回select所有被选中选项的元素列表
- first_selected_options():返回第一个被选中的选项元素
3. 取消选中项的方法
- deselect_all():取消全部的已选择项
- deselect_by_index(index):取消已选中的索引项
- deselect_by_value(value):取消已选中的value值
- deselect_by_visible_text(text):取消已选中的文本值