selenium处理select标签的下拉框
有时候我们会碰到<select></select>标签的下拉框。直接点击下拉框中的选项不一定可行。Selenium专门提供了Select类来处理下拉框。

<select id="status" class="form-control valid" onchange="" name="status">
<option value=""></option>
<option value="0">未审核</option>
<option value="1">初审通过</option>
<option value="2">复审通过</option>
<option value="3">审核不通过</option>
</select>
Python
先以python为例,查看Selenium代码select.py文件的实现:
...\selenium\webdriver\support\select.py
class Select:
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"
查看Select类的实现需要一个元素的定位。并且Example中给了例句。
Select(driver.find_element_by_tag_name("select")).select_by_index(2)
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
"""
match = str(index)
matched = False
for opt in self.options:
if opt.get_attribute("index") == match:
self._setSelected(opt)
if not self.is_multiple:
return
matched = True
if not matched:
raise NoSuchElementException("Could not locate element with index %d" % index)
继续查看select_by_index() 方法的使用并符合上面的给出的下拉框的要求,因为它要求下拉框的选项必须要有index属性,例如index=”1”。
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
"""
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)
继续查看select_by_value() 方法符合我们的需求,它用于选取<option>标签的value值。最终,可以通过下面有实现选择下拉框的选项。
from selenium.webdriver.support.select import Select ……
sel = driver.find_element_by_xpath("//select[@id='status']")
Select(sel).select_by_value('') #未审核
Select(sel).select_by_value('') #初审通过
Select(sel).select_by_value('') #复审通过
Select(sel).select_by_value('') #审核不通过
Java
当然,在java中的用法也类似,唯一不区别在语法层面有。
package com.jase.base; import org.openqa.selenium.WebDriver;
import org.openqa.selenium.By.ById;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select; public class SelectTest { public static void main(String[] args){ WebDriver driver = new ChromeDriver();
driver.get("http://www.you_url.com"); // …… Select sel = new Select(driver.findElement(ById.xpath("//select[@id='status']")));
sel.selectByValue("0"); //未审核
sel.selectByValue("1"); //初审通过
sel.selectByValue("2"); //复审通过
sel.selectByValue("3"); //审核不通过
}
}
selenium处理select标签的下拉框的更多相关文章
- Selenium: 利用select模块操作下拉框
在利用selenium进行UI自动化测试过程中,经常会遇到下拉框选项,这篇博客,就介绍下如何利用selenium的Select模块来对标准select下拉框进行操作... 首先导入Select模块: ...
- 前端 HTML form表单标签 select标签 option 下拉框
<select></select> select里面通常跟option配合使用 <!DOCTYPE html> <html lang="en&quo ...
- select标签的下拉框为图片的插件
1 参考文献: [1] https://github.com/rvera/imag...[2] https://rvera.github.io/image... [3] http://webseman ...
- jquery美化select,自定义下拉框样式
select默认的样式比较丑,有些应用需要美化select,在网上找到一个很好的美化样式效果,本人很喜欢,在这里分享一下. <!DOCTYPE html PUBLIC "-//W3C/ ...
- Selenium:利用select模块处理下拉框
在利用selenium进行UI自动化测试过程中,经常会遇到下拉框选项,这篇博客,就介绍下如何利用selenium的Select模块来对标准select下拉框进行操作... 首先导入Select模块: ...
- selenium操作隐藏的元素 (下拉框类型)
有时候我们会碰到一些元素不可见,这个时候selenium就无法对这些元素进行操作了.例如,下面的情况: Python 页面主要通过“display:none”来控制整个下拉框不可见.这个时候如果直接操 ...
- Selenium实战总结(webwiew下拉框定位)
基于常见的两种下拉框的展示形式: 1.点击弹出下拉框: 2.鼠标移动弹出下拉框(move_to_element) 实例一[鼠标点击弹出的下拉框]: e.g百度首页的设置--高级搜索--时间: 导包: ...
- Python + Selenium 定位非selected型下拉框的方法
最近在尝试给自己负责的模块写UI自动化的Demo 登录及切换页面比较顺利 但是遇到下拉框的选择时,遇到了一点困难 我负责的模块页面的下拉框并非Select类型,无法使用select_by_index ...
- element-ui select可搜索下拉框无法在IOS或Ipad调起小键盘输入法
参考:https://segmentfault.com/q/1010000021748033 原因:常规select是可以调起小键盘的.但是element-ui的select其实是input.并且这个 ...
随机推荐
- WEB开发之路——基础部分
WEB开发之路 受BBC的<BBC: Brain Story>和<BBC: The Brain - A Secret History>的影响,我一直有志于探究人类大脑,2015 ...
- iOS一些关于日历的问题
int CalculateDays(int ys, int ms, int ds, int ye, int me, int de) { int days = CalcYearRestDays(ys, ...
- Windows 自动关机/定时关机 命令 shuntdown
一 .倒计时关机: 指定系统在10分钟后自动关闭:点击"开始→运行",输入命令"Shutdown -s -t 60"(注意:引号不输入,参数之间有空格 ...
- RHEL5.8配置NFS服务
机器配置:4C+16GB 操作系统:RedHat Enterprise Linux 5.8 NFS基础 NFS(Network File System)是Linux系统之间使用最为广泛的文件共享协议, ...
- 使用statsd+graphite+grafana构建业务及性能监控模块
近些年随着DevOps概念越来越收到重视,除了传统的Splunk,Zabbix外在开源领域也有越来越多的软件可供使用.从数据收集,时序数据库,图形展示等主要方面有各类可扩展的软件用于搭建一个数据监控平 ...
- clang 简单的str_replace实现
自己写的一段: //gool char* str_replace(char* source, const char* find, const char* replace){ if (source == ...
- jQuery+ASP.NET MVC基于CORS实现带cookie的跨域ajax请求
这是今天遇到的一个实际问题,在这篇随笔中记录一下解决方法. ASP.NET Web API提供了CORS支持,但ASP.NET MVC默认不支持,需要自己动手实现.可以写一个用于实现CORS的Acti ...
- ENode 1.0 - 事件驱动架构(EDA)思想的在框架中如何体现
开源地址:https://github.com/tangxuehua/enode 上一篇文章,我给大家分享了我的一个基于DDD以及EDA架构的框架enode,但是只是介绍了一个大概.接下来我准备用很多 ...
- js只需5分钟创建一个跨三大平台纯原生APP
DeviceOne之前介绍过了,现在来介绍一下DeviceOne快速开发到什么程度 使用js只需要5分钟就可以打出垮Android.ios.windows三大平台的纯原生UI的安装包. 只需要6个小时 ...
- Asp.Net MVC 扩展 Html.ImageFor 方法详解
背景: 在Asp.net MVC中定义模型的时候,DataType有DataType.ImageUrl这个类型,但htmlhelper却无法输出一个img,当用脚手架自动生成一些form或表格的时候, ...