博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
selenium处理select标签的下拉框
阅读量:6690 次
发布时间:2019-06-25

本文共 3626 字,大约阅读时间需要 12 分钟。

 

  有时候我们会碰到<select></select>标签的下拉框。直接点击下拉框中的选项不一定可行。Selenium专门提供了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类的实现需要一个元素的定位。并且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:                      :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('0')  #未审核Select(sel).select_by_value('1')  #初审通过Select(sel).select_by_value('2')  #复审通过Select(sel).select_by_value('3')  #审核不通过

 

 

 

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"); //审核不通过    }}

 

转载地址:http://mthao.baihongyu.com/

你可能感兴趣的文章
新浪招聘的图片滚动控制JS效果
查看>>
jeecg入门操作—表单界面
查看>>
如何折分字符串技巧讨论-总结
查看>>
第24课 经典问题解析二
查看>>
Material Design 组件之NavigationView
查看>>
【Android】3.13 路径规划功能
查看>>
工作中记录一 list转树
查看>>
spring踩坑
查看>>
Delphi按名字调用方法高级解决方案
查看>>
关于对象与类型
查看>>
转:程序员面试什么最重要
查看>>
团队项目(六)- 事后诸葛亮分析(江山代有才人秃)
查看>>
Linux基本命令(二)-----vim相关命令
查看>>
一些常用的正则表达式
查看>>
UI设计,使用感知分层技术
查看>>
poj1905 Expanding Rods
查看>>
Thread 中的 中断
查看>>
【模板】三分法
查看>>
2015 多校联赛 ——HDU5416(异或)
查看>>
hdu 4533 线段树(问题转化+)
查看>>