在项目测试中遇到了下拉框选择的控件,来总结下如何使用select选择下拉框:

下图是Select类的初始化描述,意思是,给定元素是得是select类型,不是就抛异常。接下来给了例子:要操作这个select,先要定位到,然后再通过select_by_index 选择下拉框


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)
"""
if webelement.tag_name.lower() != "select":
raise UnexpectedTagNameException(
"Select only works on <select> elements, not on <%s>" %
webelement.tag_name)
self._el = webelement
multi = self._el.get_attribute("multiple")
self.is_multiple = multi and multi != "false"
 

1、select_by_value:

看下代码:
    def select_by_value(self, value):
"""Select all options that have a value matching the argument. That is, when given "foo" this
would select an option like: <option value="foo">Bar</option> :Args:
- value - The value to match against throws NoSuchElementException If there is no option with specisied value in SELECT
"""
css = "option[value =%s]" % self._escapeString(value)
opts = self._el.find_elements(By.CSS_SELECTOR, css)
matched = False
for opt in opts:
self._setSelected(opt)
if not self.is_multiple:
return
matched = True
if not matched:
raise NoSuchElementException("Cannot locate option with value: %s" % value)

就是说使用这个方法,下拉框属性需要有value,如果选项中不具有指定值的项,就抛异常。例如:

 

2、select_by_index
看下代码:
     def select_by_index(self, index):
"""Select the option at the given index. This is done by examing the "index" attribute of an
element, and not merely by counting. :Args:
- index - The option at this index will be selected throws NoSuchElementException If there is no option with specisied index in SELECT
"""
match = str(index)
for opt in self.options:
if opt.get_attribute("index") == match:
self._setSelected(opt)
return
raise NoSuchElementException("Could not locate element with index %d" % index)

这个是通过元素的“index”属性来完成


3、select_by_visible_text
看下代码:
 
     def select_by_visible_text(self, text):
"""Select all options that display text matching the argument. That is, when given "Bar" this
would select an option like: <option value="foo">Bar</option> :Args:
- text - The visible text to match against throws NoSuchElementException If there is no option with specisied text in SELECT
"""
xpath = ".//option[normalize-space(.) = %s]" % self._escapeString(text)
opts = self._el.find_elements(By.XPATH, xpath)
matched = False
for opt in opts:
self._setSelected(opt)
if not self.is_multiple:
return
matched = True if len(opts) == 0 and " " in text:
subStringWithoutSpace = self._get_longest_token(text)
if subStringWithoutSpace == "":
candidates = self.options
else:
xpath = ".//option[contains(.,%s)]" % self._escapeString(subStringWithoutSpace)
candidates = self._el.find_elements(By.XPATH, xpath)
for candidate in candidates:
if text == candidate.text:
self._setSelected(candidate)
if not self.is_multiple:
return
matched = True if not matched:
raise NoSuchElementException("Could not locate element with visible text: %s" % text)
 通过选择文本来匹配,然后给出了例子。看下我的例子:

我的代码:

    stafftype_loc = (By.XPATH, "//select[@ng-model='Invite.type']")

      find_element(*self.stafftype_loc).send_keys(stftype)
 
 

【selenium】基于python语言,如何用select选择下拉框的更多相关文章

  1. selenium select 选择下拉框

    实战百度首页设置,浏览偏好设置. 打开首页,在非登录的情况下,查看分析页面元素,我们可以看到,我们首先要点击的是设置, 接着点击,搜索设置, 然后select选择下拉框. select_by_inde ...

  2. Vue.js中使用select选择下拉框

    在Vue.js中使用select选择下拉框有两种方法: 第一种: Add.html: <select v-model="sysNotice.noticeType" id=&q ...

  3. select change下拉框改变事件 设置选定项,禁用select

    select change下拉框改变事件 设置选定项,禁用select 1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitio ...

  4. selenium 难定位元素,时间插件,下拉框定位,string

    1.元素定位 ID定位元素: findElement(By.id(“”)); 通过元素的名称定位元素: findElement(By.name(“”)); 通过元素的html中的位置定位元素: fin ...

  5. jq select change下拉框选项变化判断选中值,添加(attr)或移除(removeAttr)一个或多个属性

    select change下拉框选项变化判断选中值,添加(attr)或移除(removeAttr)一个或多个属性 $("#IsRecommend").change(function ...

  6. selenium 显示等待wait.until 常用封装 及下拉框的选择操作等

    from selenium.webdriver.common.by import Byfrom selenium.webdriver.support.ui import WebDriverWait a ...

  7. selenium - Select类 - 下拉框

    WebDriver提供了Select类来处理下拉框. 如百度搜索设置的下拉框,如下图: from selenium import webdriver from selenium.webdriver.s ...

  8. Selenium示例集锦--常见元素识别方法、下拉框、文本域及富文本框、鼠标操作、一组元素定位、弹窗、多窗口处理、JS、frame、文件上传和下载

    元素定位及其他操作 0.常见的识别元素的方法是什么? driver.find_element_by_id() driver.find_element_by_name() driver.find_ele ...

  9. selenium如何随机选取省份和城市的下拉框的值

    1.原始需求,选择省份后,相应的城市会自动加载 2.思路 a.获取下拉框的所有元素个数 b.随机点击0-元素个数之间的某个值 3.代码实现 Random random = new Random(); ...

随机推荐

  1. 解决chrome插件安装时出现的“程序包无效”问题信息:程序包无效。

    https://blog.csdn.net/bluexuemei/article/details/35213117 2014-06-27 09:00:51 bluexuemei 阅读数 14374更多 ...

  2. NLP中的预训练语言模型(三)—— XL-Net和Transformer-XL

    本篇带来XL-Net和它的基础结构Transformer-XL.在讲解XL-Net之前需要先了解Transformer-XL,Transformer-XL不属于预训练模型范畴,而是Transforme ...

  3. VS调试

    1.调试输出变量值 F9先设置断点,开始调试后,依次选择调试——>窗口——>局部变量和监视——>监视1. 点击“全部中断”——>之后局部变量会显示相关变量值,监视1可以查看变量 ...

  4. websocket广播式实例

    1.引入相关依赖 <parent> <groupId>org.springframework.boot</groupId> <artifactId>sp ...

  5. hdu3068-最长回文-马拉车(Manacher)算法

    http://acm.hdu.edu.cn/showproblem.php?pid=3068 脑子转个弯总算看懂马拉车算法了.记录一下思路和模板. 马拉车算法是在O(n)的时间内求出最大回文子串. 一 ...

  6. TP5导入EXCEL到数据库

    前期准备工作: 1.下载PHPExcel放到vendor下 2.前端页面: <form action="save" method="post" encty ...

  7. Codeforces Round #604 (Div. 2) D、E、F题解

    Beautiful Sequence Beautiful Mirrors Beautiful Bracket Sequence (easy version) Beautiful Sequence \[ ...

  8. VSCode编写C/C++语言,配置文件和注意事项

    前言 主要是自己先下载好VSCode,然后在自己电脑上安装好就行了,但是VSCode是编辑器,不是IDE,所以需要自己配置文件,主要有四个都是以.json结尾的文件,这里默认已经装过C/C++的编辑器 ...

  9. [LeetCode] 890. Find and Replace Pattern 查找和替换模式

    You have a list of words and a pattern, and you want to know which words in words matches the patter ...

  10. [LeetCode] 38. Count and Say 计数和读法

    The count-and-say sequence is the sequence of integers with the first five terms as following: 1. 1 ...