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('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"); //审核不通过
    }
}
复制代码

 

目录
相关文章
|
4月前
selenium对下拉框操作
selenium对下拉框操作
22 0
|
8月前
|
前端开发 黑灰产治理 索引
selenium-元素定位+下拉框代码实战
selenium-元素定位+下拉框代码实战
111 0
|
8月前
|
JavaScript
selenium--更改标签的属性值
selenium--更改标签的属性值
|
前端开发 JavaScript
大型情感剧集Selenium:4_老中医教你(单/多/下拉框)选项定位
讲什么标题说了,讲selenium的单选、多选、下拉框选项定位。但其实这东西,没什么太多说的,又比较枯燥,那该怎么让这一集selenium的课程变得有趣呢?有请老中医,哈哈....
196 0
|
前端开发 索引
Selenium系列(十) - 针对Select下拉框的操作和源码解读
Selenium系列(十) - 针对Select下拉框的操作和源码解读
187 0
|
数据采集 索引 Python
python爬虫selenium操作下拉框详解
python爬虫selenium操作下拉框详解
584 0
python爬虫selenium操作下拉框详解
Python+Selenium 技巧篇-svg标签内元素的xpath定位方式
Python+Selenium 技巧篇-svg标签内元素的xpath定位方式
1019 0
Python+Selenium 技巧篇-svg标签内元素的xpath定位方式
|
索引 Python
Selenium2+python自动化15-select下拉框
前言 最近由于工作原因,更新慢了一点,今天终于抽出一点时间给大家继续更新selenium系列,学习的脚本不能停止,希望小伙伴能多多支持。 本篇以百度设置下拉选项框为案例,详细介绍select下拉框相关的操作方法。
1471 0
C#中通过Selenium定位<a>标签的问题
刚才在QQ群里看到有人提问,如何实现退出百度登录问题。那么之所以会有这个问题,主要是因为这个元素,如下图所示,是无法直接定位到的: 经过研究发现,要想定位到这种元素,拢共分两步: 第一步,把鼠标移到能使目标元素显示在页面上的前置元素上; 第二步,通过xpath对目标标签元素进行定位。
1361 0

热门文章

最新文章