如果你还想从头学起Selenium,可以看看这个系列的文章哦!

https://www.cnblogs.com/poloyy/category/1680176.html

其次,如果你不懂前端基础知识,需要自己去补充哦,博主暂时没有总结(虽然我也会,所以我学selenium就不用复习前端了哈哈哈...)

首先,将下面html代码保存到一个文件中

后续的代码小案例都是访问此html的<!DOCTYPE html>

<html lang="en">
<head>
<meta charset="UTF-8">
<title>下拉框</title>
</head>
<body> <select id="pro">
<option value="gd">广东</option>
<option value="hb">湖北</option>
<option value="gj">北京</option>
</select> <select id="city" multiple>
<option value="gz">广州</option>
<option value="wh">武汉</option>
<option value="gj">北京</option>
</select>
</body>
</html>

注意

若select下拉框有 multiple 属性,则可以多选option,但这种情况不常见

关于下拉框的操作

  • 返回所有选项
  • 返回所有被选中的选项
  • 通过value属性选中or取消选中选项
  • 通过index索引选中or取消选中选项
  • 通过标签间文本选中or取消选中选项
  • 取消选中所有选项

返回选项&选中操作

# !/usr/bin/env python
# -*- coding: utf-8 -*- """
__title__ =
__Time__ = 2020/3/25 17:52
__Author__ = 小菠萝测试笔记
__Blog__ = https://www.cnblogs.com/poloyy/
"""
from time import sleep from selenium.webdriver.support.select import Select
from selenium import webdriver driver = webdriver.Chrome("../resources/chromedriver.exe") # 将html文件更改为自己的路径
driver.get("file:///C:/下拉框.html")
driver.maximize_window() # 找到select标签元素
pro = Select(driver.find_element_by_id("pro")) # 返回所有选项
for option in pro.options:
print(option.text) # 返回所有被选中的选项
for option in pro.all_selected_options:
print(option.text) # 通过value选中
pro.select_by_value("bj")
sleep(1) # 通过index选中
pro.select_by_index(1)
sleep(1) # 通过标签文本选中
pro.select_by_visible_text("广东")

取消选中操作

# 找到id=city的下拉框
city = Select(driver.find_element_by_id("city")) # 全选
for option in city.options:
if not option.is_selected():
city.select_by_visible_text(option.text)
sleep(1) # 根据value取消选中
city.deselect_by_value("bj")
sleep(1) # 根据index取消选中
city.deselect_by_index(0)
sleep(1) # 根据标签文本选中
city.deselect_by_visible_text("武汉")
sleep(1) # 全选
for option in city.options:
if not option.is_selected():
city.select_by_visible_text(option.text)
sleep(1) # 取消选中所有选项
city.deselect_all()

知识点

取消操作只适用于添加了multiple的下拉框,否则会报错

    raise NotImplementedError("You may only deselect options of a multi-select")
NotImplementedError: You may only deselect options of a multi-select

Select源码解读

class Select(object):

    def __init__(self, webelement):
"""
Constructor. A check is made that the given element is, indeed, a SELECT tag. If it is not,
then an UnexpectedTagNameException is thrown. :Args:
- webelement - element SELECT element to wrap Example:
from selenium.webdriver.support.ui import Select \n
Select(driver.find_element_by_tag_name("select")).select_by_index(2)
"""

知识点

  • 实例化 Select 需要传入 select 下拉框的 webelement
  • 若传入 webelement 的 tag_name 不是 <select>..</select> 标签,则会抛出异常 UnexpectedTagNameException

最新文章

  1. @font-face使用
  2. Windows Live Writer 的昨日荣光
  3. servlet总结
  4. java中用过滤器解决字符编码问题
  5. 测试卡尔曼滤波器(Kalman Filter)
  6. 几个gcc的扩展功能
  7. Java的访问控制
  8. 解决spawn-fcgi child exited with: 1
  9. 山寨小小军团开发笔记 之 Arrow Projectile
  10. 各加密模式的演示(ECB,CBC)
  11. 集合用法笔记-Map用法
  12. [Alpha阶段]第三次Scrum Meeting
  13. java学习-- String
  14. [Luogu4986] 逃离
  15. Android MIME类型结构
  16. 3.1_分类算法之k-近邻
  17. np.random.seed()
  18. Beta 冲刺 (3/7)
  19. 与平台无关的类型,int8_t,uint8_t
  20. 随笔记:如何使用Python连接(/操作)Oracle数据库(Windows平台下)

热门文章

  1. Sed 实记 &middot; laoless's Blog
  2. 【原创】面试官问我G1回收器怎么知道你是什么时候的垃圾?
  3. 原生js中的常用方法的写法
  4. idea 2018.1激活方法
  5. 负margin在页面布局中的应用
  6. agent判断用户请求设备
  7. mysql的锁与事务
  8. CSS 权重图
  9. WEB应用之http协议和httpd闲聊
  10. LSTM + linear-CRF序列标注笔记