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

如果你还想从头学起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

原文地址:https://www.cnblogs.com/poloyy/p/12601101.html

时间: 2024-08-22 09:20:28

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

selenium webdriver学习(八)------------如何操作select下拉框(转)

selenium webdriver学习(八)------------如何操作select下拉框 博客分类: Selenium-webdriver 下面我们来看一下selenium webdriver是如何来处理select下拉框的,以http://passport.51.com/reg2.5p这个页面为例.这个页面中有4个下拉框,下面演示4种选中下拉框选项的方法.select处理比较简单,直接看代码吧:) Java代码   import org.openqa.selenium.By; impo

Python3.x:selenium遍历select下拉框获取value值

Python3.x:selenium遍历select下拉框获取value值 Select提供了三种选择方法: # 通过选项的顺序,第一个为 0 select_by_index(index) # 通过value属性 select_by_value(value) # 通过选项可见文本 select_by_visible_text(text) Select提供了四种方法取消选择: deselect_by_index(index) deselect_by_value(value) deselect_by

selenium (三) 下拉框选项操作

对下拉框操作的方式其实有多种,可以先通过find_elements_by_xpath()获取到下拉框中的所有选项列表,然后在通过list元素进行click()来选择选项(这是我最初实现对下拉框操作的方式),也可以使用selenium自带的方法实现下拉框选项的操作.两者其实差不多,不过,个人觉得还是用find_elements_by_xpath()方式更具有扩展性.因为有些下拉框中的选项中,有可选和不可选的选项时,find_elements_by_xpath()可以通过元素属性过滤掉不可选的选项,

JavaScript获取Select下拉框Option的Value和Text值的方法

Js获取select下拉列表框各个Option的Value值相对比较容易,不过获取Text值却有点麻烦,对于一个初学JavaScript的 新手来说,可能一时还无从下手,那么就请看下本文的方法,以一个form表单中的Select下拉框菜单为例,来说明如何用JavaScript获取其 Value值和Text值: 示例表单,里面是一个select下拉列表框的各个列表项及值: <form name="form1"> <select name="testvalue&

Javascript获取select下拉框选中的的值

现在有一id=test的下拉框,怎么拿到选中的那个值呢? 分别使用javascript原生的方法和jquery方法 <select id="test"  name="">     <option   value="1">text1</option>     <option   value="2">text2</option>    </select> co

jQuery制作简洁的多级联动Select下拉框

今天我们要来分享一款很实用的jQuery插件,它是一个基于jQuery多级联动的省市地区Select下拉框,并且值得一提的是,这款联动下拉框是经过自定义美化过的,外观比浏览器自带的要漂亮许多.另外,这个Select下拉框也可以绑定下拉事件,并获取当前选中项的值. html代码: <div class="wrap">        <div class="nice-select" name="nice-select">   

模拟select下拉框之多选(数据源采用模拟Ajax数据--原创)

最近需要一个下拉多选,本来想偷懒的,所以在网上百度了一番,最终还是发现没有一个符合自己要求的,所以我自己写了一个插件.下面是GIF动态效果图展示 相信大家已经看到效果了,接下来就是我的代码展示 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>模拟select下拉框之多选</title> <style

去除select下拉框默认样式

去除select下拉框默认样式 select { /*Chrome和Firefox里面的边框是不一样的,所以复写了一下*/ border: solid 1px #000; /*很关键:将默认的select选择框样式清除*/ appearance:none; -moz-appearance:none; -webkit-appearance:none; /*将背景改为红色*/ background:red; /*加padding防止文字覆盖*/ padding-right: 14px; } /*清除

基于jQuery select下拉框美化插件

今天给大家分享一款基于jQuery select下拉框美化插件,这款插件适用浏览器:IE8.360.FireFox.Chrome.Safari.Opera.傲游.搜狗.世界之窗.  <table>         <tr>             <td>                 <h2>                     演示1</h2>                 <select name="drop1&qu