selenium处理select标签的下拉框

1.div下拉框的处理

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

selenium处理select标签的下拉框_第1张图片
如:现在我们来通过脚本选择下拉列表里的 $10.69

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

2.select标签

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

selenium处理select标签的下拉框_第2张图片

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