有时候我们会碰到<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"); //审核不通过
}
}

最新文章

  1. Linux 下编译升级 Python
  2. NopCommerce 增加 Customer Settings
  3. Luogu 3396 权值分块
  4. HDU 5937 Equation
  5. Windows Server 2008 R2 IIS7.5下PHP、MySQL快速环境配置【图】
  6. 如何正确的将J2ee项目部署到Tomcat
  7. 仅IE6中链接A的href为javascript协议时不能在当前页面跳转
  8. mysql 查看最大连接数 设置最大连接数
  9. Typecho集成ueditor笔记
  10. 学习mysql语法--基础篇(二)
  11. Linux逻辑卷管理器concept
  12. 市场主流5款HTML5开发框架详解
  13. 强化学习(十四) Actor-Critic
  14. 从 Python 快速启动 CGI 服务器
  15. 关于使用freemarker导出文档的使用
  16. (admin.E108) The value of &#39;list_display[4]&#39;报错解决方案
  17. 如何利用mui实现底部选择器(含日期选择器)?
  18. 线特征---Edge Drawing(七)
  19. estimator = KerasClassifier
  20. MySQL(六)创建用户与授权

热门文章

  1. 解决ie8(及其以下)不支持getElementsByClassName的问题
  2. NTP时间同步
  3. 《理解 ES6》阅读整理:函数(Functions)(五)Name Property
  4. angular2 递归导航菜单实现方式
  5. centos7 memcached+memagent 集群
  6. centos 6.5 中部署开源的Lepus(天兔)监控
  7. 设计模式之美:Dynamic Property(动态属性)
  8. DOM扩展札记
  9. Java提高篇(三一)-----Stack
  10. 在tomcat下部署工程