【问题标题】:How to get selected option value in protractor如何在量角器中获取选定的选项值
【发布时间】:2017-12-01 07:10:31
【问题描述】:

如何获取当前选择框的选项值?

我尝试了下面的代码,但它不起作用。

element(by.css('#selectbox')).all(by.tagName('option')).then(function (options) {
    options.forEach(option => {
        if (option.getAttribute('selected')) {
            console.log('selected option', option);
        }
    });
});

任何帮助将不胜感激。

【问题讨论】:

标签: angularjs protractor selectedvalue


【解决方案1】:

是“选定”和属性还是类值?

它可以作为“获取被选中的元素,然后读出它的值”

在代码中:

//if selected is an attribute
element(by.css('#selectbox option[selected]')).getAttribute('value').then(function(value){
    console.log('selected option is '+value);
})

//if selected is a class-attribute
element(by.css('#selectbox option.selected')).getAttribute('value').then(function(value){
    console.log('selected option is '+value);
})

//note, that "element(by.css())" === "$()", so this is the same as the first one
$('#selectbox option[selected]').getAttribute('value').then(function(value){
    console.log('selected option is '+value);
})

Read more here about locators 更重要的是here about CSS-Selectors

【讨论】:

    【解决方案2】:

    您可以使用.filter() 过滤掉选定的选项,或者直接使用 CSS 选择器定位它:

    element(by.css('#selectbox option[selected]'))
    

    请注意,在 Protractor 中有一种更好的方式来处理/交互select:

    【讨论】:

      猜你喜欢
      • 2020-12-25
      • 1970-01-01
      • 2018-11-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-15
      • 2017-11-28
      • 2019-01-23
      相关资源
      最近更新 更多