选择->选项抽象

发布于 2021-01-29 14:10:27

在Python,Java和其他几种硒绑定中,在select->optionHTML结构(Selectclass)上有一个非常方便的抽象。

例如,假设有以下select标记:

<select id="fruits" class="select" name="fruits">
    <option value="1">Banana</option>
    <option value="2">Mango</option>
</select>

这是我们如何在Python中操作它的方法:

from selenium.webdriver.support.ui import Select

select = Select(driver.find_element_by_id('fruits'))

# get all options
print select.options

# get all selected options
print select.all_selected_options

# select an option by value
select.select_by_value('1')

# select by visible text
select.select_by_visible_text('Mango')

换句话说,这是一个 非常透明且易于使用的抽象

可以类似的方式 在量角器* 中操纵select标签吗? *


与下拉式量角器e2e测试中的“如何选择选项”量角器测试中的“选择”框中的如何单击选项不是重复的

关注者
0
被浏览
104
1 个回答
  • 面试哥
    面试哥 2021-01-29
    为面试而生,有面试问题,就找面试哥。

    在量角器中没有这样的东西,但是我们可以编写自己的东西:

    select-wrapper.js

    'use strict';
    
    var SelectWrapper = function(selector) {
        this.webElement = element(selector);
    };
    SelectWrapper.prototype.getOptions = function() {
        return this.webElement.all(by.tagName('option'));
    };
    SelectWrapper.prototype.getSelectedOptions = function() {
        return this.webElement.all(by.css('option[selected="selected"]'));
    };
    SelectWrapper.prototype.selectByValue = function(value) {
        return this.webElement.all(by.css('option[value="' + value + '"]')).click();
    };
    SelectWrapper.prototype.selectByPartialText = function(text) {
        return this.webElement.all(by.cssContainingText('option', text)).click();   
    };
    SelectWrapper.prototype.selectByText = function(text) {
        return this.webElement.all(by.xpath('option[.="' + text + '"]')).click();   
    };
    
    module.exports = SelectWrapper;
    

    用法

    var SelectWrapper  = require('select-wrapper');
    var mySelect = new SelectWrapper(by.id('fruits'));
    
    # select an option by value
    mySelect.selectByValue('1');
    
    # select by visible text
    mySelect.selectByText('Mango');
    

    请注意, Select
    JavaScript中保留字



知识点
面圈网VIP题库

面圈网VIP题库全新上线,海量真题题库资源。 90大类考试,超10万份考试真题开放下载啦

去下载看看