前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Selenium系列(十) - 针对Select下拉框的操作和源码解读

Selenium系列(十) - 针对Select下拉框的操作和源码解读

作者头像
小菠萝测试笔记
发布2020-06-09 15:33:30
7870
发布2020-06-09 15:33:30
举报

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

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

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

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

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

代码语言:javascript
复制
<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取消选中选项
  • 取消选中所有选项

返回选项&选中操作

代码语言:javascript
复制
# !/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("广东")

取消选中操作

代码语言:javascript
复制
# 找到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的下拉框,否则会报错

代码语言:javascript
复制
    raise NotImplementedError("You may only deselect options of a multi-select")
NotImplementedError: You may only deselect options of a multi-select

Select源码解读

代码语言:javascript
复制
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 下拉框的 webelement

Select

  • 若传入 的 tag_name 不是 <select>..</select> 标签,则会抛出异常 UnexpectedTagNameException

webelement

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-03-30 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 首先,将下面html代码保存到一个文件中
    • 注意
    • 关于下拉框的操作
    • 返回选项&选中操作
    • 取消选中操作
      • 知识点
      • Select源码解读
        • 知识点
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档