如何获取量角器中选定的选项值

2023-11-29

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

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

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

任何帮助,将不胜感激。


“选择”是属性还是类值?

它将作为“获取选定的元素,然后读出其值”

In code:

//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);
})

在此处阅读有关定位器的更多信息更重要的是这里关于CSS选择器

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何获取量角器中选定的选项值 的相关文章

随机推荐