selenium处理select标签的下拉框

1.div下拉框的处理

div下拉框与其他元素一样,没有啥特殊的,只需要用xpath来定位即可。

这里写图片描述
如:现在我们来通过脚本选择下拉列表里的 $10.69

#先定位到下拉框
m=driver.find_element_by_id("ShippingMethod")
#再点击下拉框下的选项
m.find_element_by_xpath("//option[@value='10.69']").click()
time.sleep(3)

2.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

select类
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)

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 
           """
        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”。

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
           """
        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')  #审核不通过

再来一个例子
现在我们来通过脚本选择下拉列表里的 “应用物理”:
这里写图片描述

##先定位到下拉框
type = driver.find_element_by_xpath(".//*[@id='album_create_form']/ul/li[6]/div[2]/select")
##再选择下拉框下的选项
Select(type).select_by_value("应用物理")
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值